Debian Codegraph @

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code to emit Expr nodes as CIR code.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "Address.h"
14 #include "CIRGenConstantEmitter.h"
15 #include "CIRGenFunction.h"
16 #include "CIRGenModule.h"
17 #include "CIRGenValue.h"
18 #include "mlir/IR/BuiltinAttributes.h"
19 #include "mlir/IR/Value.h"
20 #include "clang/AST/Attr.h"
21 #include "clang/AST/CharUnits.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/ExprCXX.h"
25 #include "clang/Basic/AddressSpaces.h"
26 #include "clang/Basic/TargetInfo.h"
27 #include "clang/CIR/Dialect/IR/CIRAttrs.h"
28 #include "clang/CIR/Dialect/IR/CIRDialect.h"
29 #include "clang/CIR/Dialect/IR/CIRTypes.h"
30 #include "clang/CIR/MissingFeatures.h"
31 #include <optional>
32
33 using namespace clang;
34 using namespace clang::CIRGen;
35 using namespace cir;
36
37 /// Get the address of a zero-sized field within a record. The resulting address
38 /// doesn't necessarily have the right type.
39 Address CIRGenFunction::emitAddrOfFieldStorage(Address base,
40 const FieldDecl *field,
41 llvm::StringRef fieldName,
42 unsigned fieldIndex) {
43 if (field->isZeroSize(getContext())) {
44 cgm.errorNYI(field->getSourceRange(),
45 "emitAddrOfFieldStorage: zero-sized field");
46 return Address::invalid();
47 }
48
49 mlir::Location loc = getLoc(field->getLocation());
50
51 mlir::Type fieldType = convertType(field->getType());
52 auto fieldPtr = cir::PointerType::get(fieldType);
53 // For most cases fieldName is the same as field->getName() but for lambdas,
54 // which do not currently carry the name, so it can be passed down from the
55 // CaptureStmt.
56 cir::GetMemberOp memberAddr = builder.createGetMember(
57 loc, fieldPtr, base.getPointer(), fieldName, fieldIndex);
58
59 // Retrieve layout information, compute alignment and return the final
60 // address.
61 const RecordDecl *rec = field->getParent();
62 const CIRGenRecordLayout &layout = cgm.getTypes().getCIRGenRecordLayout(rec);
63 unsigned idx = layout.getCIRFieldNo(field);
64 CharUnits offset = CharUnits::fromQuantity(
65 layout.getCIRType().getElementOffset(cgm.getDataLayout().layout, idx));
66 return Address(memberAddr, base.getAlignment().alignmentAtOffset(offset));
67 }
68
69 /// Given an expression of pointer type, try to
70 /// derive a more accurate bound on the alignment of the pointer.
71 Address CIRGenFunction::emitPointerWithAlignment(const Expr *expr,
72 LValueBaseInfo *baseInfo) {
73 // We allow this with ObjC object pointers because of fragile ABIs.
74 assert(expr->getType()->isPointerType() ||
75 expr->getType()->isObjCObjectPointerType());
76 expr = expr->IgnoreParens();
77
78 // Casts:
79 if (auto const *ce = dyn_cast<CastExpr>(expr)) {
80 if (const auto *ece = dyn_cast<ExplicitCastExpr>(ce))
81 cgm.emitExplicitCastExprType(ece);
82
83 switch (ce->getCastKind()) {
84 // Non-converting casts (but not C's implicit conversion from void*).
85 case CK_BitCast:
86 case CK_NoOp:
87 case CK_AddressSpaceConversion: {
88 if (const auto *ptrTy =
89 ce->getSubExpr()->getType()->getAs<PointerType>()) {
90 if (ptrTy->getPointeeType()->isVoidType())
91 break;
92
93 LValueBaseInfo innerBaseInfo;
94 assert(!cir::MissingFeatures::opTBAA());
95 Address addr =
96 emitPointerWithAlignment(ce->getSubExpr(), &innerBaseInfo);
97 if (baseInfo)
98 *baseInfo = innerBaseInfo;
99
100 if (isa<ExplicitCastExpr>(ce)) {
101 LValueBaseInfo targetTypeBaseInfo;
102
103 const QualType pointeeType = expr->getType()->getPointeeType();
104 const CharUnits align =
105 cgm.getNaturalTypeAlignment(pointeeType, &targetTypeBaseInfo);
106
107 // If the source l-value is opaque, honor the alignment of the
108 // casted-to type.
109 if (innerBaseInfo.getAlignmentSource() != AlignmentSource::Decl) {
110 if (baseInfo)
111 baseInfo->mergeForCast(targetTypeBaseInfo);
112 addr = Address(addr.getPointer(), addr.getElementType(), align);
113 }
114 }
115
116 assert(!cir::MissingFeatures::sanitizers());
117
118 const mlir::Type eltTy =
119 convertTypeForMem(expr->getType()->getPointeeType());
120 addr = getBuilder().createElementBitCast(getLoc(expr->getSourceRange()),
121 addr, eltTy);
122 assert(!cir::MissingFeatures::addressSpace());
123
124 return addr;
125 }
126 break;
127 }
128
129 // Array-to-pointer decay. TODO(cir): BaseInfo and TBAAInfo.
130 case CK_ArrayToPointerDecay:
131 return emitArrayToPointerDecay(ce->getSubExpr(), baseInfo);
132
133 case CK_UncheckedDerivedToBase:
134 case CK_DerivedToBase: {
135 assert(!cir::MissingFeatures::opTBAA());
136 assert(!cir::MissingFeatures::addressIsKnownNonNull());
137 Address addr = emitPointerWithAlignment(ce->getSubExpr(), baseInfo);
138 const CXXRecordDecl *derived =
139 ce->getSubExpr()->getType()->getPointeeCXXRecordDecl();
140 return getAddressOfBaseClass(addr, derived, ce->path(),
141 shouldNullCheckClassCastValue(ce),
142 ce->getExprLoc());
143 }
144
145 case CK_AnyPointerToBlockPointerCast:
146 case CK_BaseToDerived:
147 case CK_BaseToDerivedMemberPointer:
148 case CK_BlockPointerToObjCPointerCast:
149 case CK_BuiltinFnToFnPtr:
150 case CK_CPointerToObjCPointerCast:
151 case CK_DerivedToBaseMemberPointer:
152 case CK_Dynamic:
153 case CK_FunctionToPointerDecay:
154 case CK_IntegralToPointer:
155 case CK_LValueToRValue:
156 case CK_LValueToRValueBitCast:
157 case CK_NullToMemberPointer:
158 case CK_NullToPointer:
159 case CK_ReinterpretMemberPointer:
160 // Common pointer conversions, nothing to do here.
161 // TODO: Is there any reason to treat base-to-derived conversions
162 // specially?
163 break;
164
165 case CK_ARCConsumeObject:
166 case CK_ARCExtendBlockObject:
167 case CK_ARCProduceObject:
168 case CK_ARCReclaimReturnedObject:
169 case CK_AtomicToNonAtomic:
170 case CK_BooleanToSignedIntegral:
171 case CK_ConstructorConversion:
172 case CK_CopyAndAutoreleaseBlockObject:
173 case CK_Dependent:
174 case CK_FixedPointCast:
175 case CK_FixedPointToBoolean:
176 case CK_FixedPointToFloating:
177 case CK_FixedPointToIntegral:
178 case CK_FloatingCast:
179 case CK_FloatingComplexCast:
180 case CK_FloatingComplexToBoolean:
181 case CK_FloatingComplexToIntegralComplex:
182 case CK_FloatingComplexToReal:
183 case CK_FloatingRealToComplex:
184 case CK_FloatingToBoolean:
185 case CK_FloatingToFixedPoint:
186 case CK_FloatingToIntegral:
187 case CK_HLSLAggregateSplatCast:
188 case CK_HLSLArrayRValue:
189 case CK_HLSLElementwiseCast:
190 case CK_HLSLVectorTruncation:
191 case CK_HLSLMatrixTruncation:
192 case CK_IntToOCLSampler:
193 case CK_IntegralCast:
194 case CK_IntegralComplexCast:
195 case CK_IntegralComplexToBoolean:
196 case CK_IntegralComplexToFloatingComplex:
197 case CK_IntegralComplexToReal:
198 case CK_IntegralRealToComplex:
199 case CK_IntegralToBoolean:
200 case CK_IntegralToFixedPoint:
201 case CK_IntegralToFloating:
202 case CK_LValueBitCast:
203 case CK_MatrixCast:
204 case CK_MemberPointerToBoolean:
205 case CK_NonAtomicToAtomic:
206 case CK_ObjCObjectLValueCast:
207 case CK_PointerToBoolean:
208 case CK_PointerToIntegral:
209 case CK_ToUnion:
210 case CK_ToVoid:
211 case CK_UserDefinedConversion:
212 case CK_VectorSplat:
213 case CK_ZeroToOCLOpaqueType:
214 llvm_unreachable("unexpected cast for emitPointerWithAlignment");
215 }
216 }
217
218 // Unary &
219 if (const UnaryOperator *uo = dyn_cast<UnaryOperator>(expr)) {
220 // TODO(cir): maybe we should use cir.unary for pointers here instead.
221 if (uo->getOpcode() == UO_AddrOf) {
222 LValue lv = emitLValue(uo->getSubExpr());
223 if (baseInfo)
224 *baseInfo = lv.getBaseInfo();
225 assert(!cir::MissingFeatures::opTBAA());
226 return lv.getAddress();
227 }
228 }
229
230 // std::addressof and variants.
231 if (auto const *call = dyn_cast<CallExpr>(expr)) {
232 switch (call->getBuiltinCallee()) {
233 default:
234 break;
235 case Builtin::BIaddressof:
236 case Builtin::BI__addressof:
237 case Builtin::BI__builtin_addressof: {
238 cgm.errorNYI(expr->getSourceRange(),
239 "emitPointerWithAlignment: builtin addressof");
240 return Address::invalid();
241 }
242 }
243 }
244
245 // Otherwise, use the alignment of the type.
246 return makeNaturalAddressForPointer(
247 emitScalarExpr(expr), expr->getType()->getPointeeType(), CharUnits(),
248 /*forPointeeType=*/true, baseInfo);
249 }
250
251 void CIRGenFunction::emitStoreThroughLValue(RValue src, LValue dst,
252 bool isInit) {
253 if (!dst.isSimple()) {
254 if (dst.isVectorElt()) {
255 // Read/modify/write the vector, inserting the new element
256 const mlir::Location loc = dst.getVectorPointer().getLoc();
257 const mlir::Value vector =
258 builder.createLoad(loc, dst.getVectorAddress());
259 const mlir::Value newVector = cir::VecInsertOp::create(
260 builder, loc, vector, src.getValue(), dst.getVectorIdx());
261 builder.createStore(loc, newVector, dst.getVectorAddress());
262 return;
263 }
264
265 assert(dst.isBitField() && "Unknown LValue type");
266 emitStoreThroughBitfieldLValue(src, dst);
267 return;
268
269 cgm.errorNYI(dst.getPointer().getLoc(),
270 "emitStoreThroughLValue: non-simple lvalue");
271 return;
272 }
273
274 assert(!cir::MissingFeatures::opLoadStoreObjC());
275
276 assert(src.isScalar() && "Can't emit an aggregate store with this method");
277 emitStoreOfScalar(src.getValue(), dst, isInit);
278 }
279
280 static LValue emitGlobalVarDeclLValue(CIRGenFunction &cgf, const Expr *e,
281 const VarDecl *vd) {
282 QualType t = e->getType();
283
284 // If it's thread_local, emit a call to its wrapper function instead.
285 if (vd->getTLSKind() == VarDecl::TLS_Dynamic)
286 cgf.cgm.errorNYI(e->getSourceRange(),
287 "emitGlobalVarDeclLValue: thread_local variable");
288
289 // Check if the variable is marked as declare target with link clause in
290 // device codegen.
291 if (cgf.getLangOpts().OpenMP)
292 cgf.cgm.errorNYI(e->getSourceRange(), "emitGlobalVarDeclLValue: OpenMP");
293
294 // Traditional LLVM codegen handles thread local separately, CIR handles
295 // as part of getAddrOfGlobalVar.
296 mlir::Value v = cgf.cgm.getAddrOfGlobalVar(vd);
297
298 assert(!cir::MissingFeatures::addressSpace());
299 mlir::Type realVarTy = cgf.convertTypeForMem(vd->getType());
300 cir::PointerType realPtrTy = cgf.getBuilder().getPointerTo(realVarTy);
301 if (realPtrTy != v.getType())
302 v = cgf.getBuilder().createBitcast(v.getLoc(), v, realPtrTy);
303
304 CharUnits alignment = cgf.getContext().getDeclAlign(vd);
305 Address addr(v, realVarTy, alignment);
306 LValue lv;
307 if (vd->getType()->isReferenceType())
308 cgf.cgm.errorNYI(e->getSourceRange(),
309 "emitGlobalVarDeclLValue: reference type");
310 else
311 lv = cgf.makeAddrLValue(addr, t, AlignmentSource::Decl);
312 assert(!cir::MissingFeatures::setObjCGCLValueClass());
313 return lv;
314 }
315
316 void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
317 bool isVolatile, QualType ty,
318 LValueBaseInfo baseInfo, bool isInit,
319 bool isNontemporal) {
320
321 if (const auto *clangVecTy = ty->getAs<clang::VectorType>()) {
322 // Boolean vectors use `iN` as storage type.
323 if (clangVecTy->isExtVectorBoolType())
324 cgm.errorNYI(addr.getPointer().getLoc(),
325 "emitStoreOfScalar ExtVectorBoolType");
326
327 // Handle vectors of size 3 like size 4 for better performance.
328 const mlir::Type elementType = addr.getElementType();
329 const auto vecTy = cast<cir::VectorType>(elementType);
330
331 // TODO(CIR): Use `ABIInfo::getOptimalVectorMemoryType` once it upstreamed
332 assert(!cir::MissingFeatures::cirgenABIInfo());
333 if (vecTy.getSize() == 3 && !getLangOpts().PreserveVec3Type)
334 cgm.errorNYI(addr.getPointer().getLoc(),
335 "emitStoreOfScalar Vec3 & PreserveVec3Type disabled");
336 }
337
338 value = emitToMemory(value, ty);
339
340 assert(!cir::MissingFeatures::opLoadStoreTbaa());
341 LValue atomicLValue = LValue::makeAddr(addr, ty, baseInfo);
342 if (ty->isAtomicType() ||
343 (!isInit && isLValueSuitableForInlineAtomic(atomicLValue))) {
344 emitAtomicStore(RValue::get(value), atomicLValue, isInit);
345 return;
346 }
347
348 // Update the alloca with more info on initialization.
349 assert(addr.getPointer() && "expected pointer to exist");
350 auto srcAlloca = addr.getDefiningOp<cir::AllocaOp>();
351 if (currVarDecl && srcAlloca) {
352 const VarDecl *vd = currVarDecl;
353 assert(vd && "VarDecl expected");
354 if (vd->hasInit())
355 srcAlloca.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
356 }
357
358 assert(currSrcLoc && "must pass in source location");
359 builder.createStore(*currSrcLoc, value, addr, isVolatile);
360
361 if (isNontemporal) {
362 cgm.errorNYI(addr.getPointer().getLoc(), "emitStoreOfScalar nontemporal");
363 return;
364 }
365
366 assert(!cir::MissingFeatures::opTBAA());
367 }
368
369 // TODO: Replace this with a proper TargetInfo function call.
370 /// Helper method to check if the underlying ABI is AAPCS
371 static bool isAAPCS(const TargetInfo &targetInfo) {
372 return targetInfo.getABI().starts_with("aapcs");
373 }
374
375 mlir::Value CIRGenFunction::emitStoreThroughBitfieldLValue(RValue src,
376 LValue dst) {
377
378 const CIRGenBitFieldInfo &info = dst.getBitFieldInfo();
379 mlir::Type resLTy = convertTypeForMem(dst.getType());
380 Address ptr = dst.getBitFieldAddress();
381
382 bool useVoaltile = cgm.getCodeGenOpts().AAPCSBitfieldWidth &&
383 dst.isVolatileQualified() &&
384 info.volatileStorageSize != 0 && isAAPCS(cgm.getTarget());
385
386 mlir::Value dstAddr = dst.getAddress().getPointer();
387
388 return builder.createSetBitfield(dstAddr.getLoc(), resLTy, ptr,
389 ptr.getElementType(), src.getValue(), info,
390 dst.isVolatileQualified(), useVoaltile);
391 }
392
393 RValue CIRGenFunction::emitLoadOfBitfieldLValue(LValue lv, SourceLocation loc) {
394 const CIRGenBitFieldInfo &info = lv.getBitFieldInfo();
395
396 // Get the output type.
397 mlir::Type resLTy = convertType(lv.getType());
398 Address ptr = lv.getBitFieldAddress();
399
400 bool useVoaltile = lv.isVolatileQualified() && info.volatileOffset != 0 &&
401 isAAPCS(cgm.getTarget());
402
403 mlir::Value field =
404 builder.createGetBitfield(getLoc(loc), resLTy, ptr, ptr.getElementType(),
405 info, lv.isVolatile(), useVoaltile);
406 assert(!cir::MissingFeatures::opLoadEmitScalarRangeCheck() && "NYI");
407 return RValue::get(field);
408 }
409
410 Address CIRGenFunction::getAddrOfBitFieldStorage(LValue base,
411 const FieldDecl *field,
412 mlir::Type fieldType,
413 unsigned index) {
414 mlir::Location loc = getLoc(field->getLocation());
415 cir::PointerType fieldPtr = cir::PointerType::get(fieldType);
416 auto rec = cast<cir::RecordType>(base.getAddress().getElementType());
417 cir::GetMemberOp sea = getBuilder().createGetMember(
418 loc, fieldPtr, base.getPointer(), field->getName(),
419 rec.isUnion() ? field->getFieldIndex() : index);
420 CharUnits offset = CharUnits::fromQuantity(
421 rec.getElementOffset(cgm.getDataLayout().layout, index));
422 return Address(sea, base.getAlignment().alignmentAtOffset(offset));
423 }
424
425 LValue CIRGenFunction::emitLValueForBitField(LValue base,
426 const FieldDecl *field) {
427 LValueBaseInfo baseInfo = base.getBaseInfo();
428 const CIRGenRecordLayout &layout =
429 cgm.getTypes().getCIRGenRecordLayout(field->getParent());
430 const CIRGenBitFieldInfo &info = layout.getBitFieldInfo(field);
431
432 assert(!cir::MissingFeatures::preservedAccessIndexRegion());
433
434 unsigned idx = layout.getCIRFieldNo(field);
435 Address addr = getAddrOfBitFieldStorage(base, field, info.storageType, idx);
436
437 mlir::Location loc = getLoc(field->getLocation());
438 if (addr.getElementType() != info.storageType)
439 addr = builder.createElementBitCast(loc, addr, info.storageType);
440
441 QualType fieldType =
442 field->getType().withCVRQualifiers(base.getVRQualifiers());
443 // TODO(cir): Support TBAA for bit fields.
444 assert(!cir::MissingFeatures::opTBAA());
445 LValueBaseInfo fieldBaseInfo(baseInfo.getAlignmentSource());
446 return LValue::makeBitfield(addr, info, fieldType, fieldBaseInfo);
447 }
448
449 LValue CIRGenFunction::emitLValueForField(LValue base, const FieldDecl *field) {
450 LValueBaseInfo baseInfo = base.getBaseInfo();
451
452 if (field->isBitField())
453 return emitLValueForBitField(base, field);
454
455 QualType fieldType = field->getType();
456 const RecordDecl *rec = field->getParent();
457 AlignmentSource baseAlignSource = baseInfo.getAlignmentSource();
458 LValueBaseInfo fieldBaseInfo(getFieldAlignmentSource(baseAlignSource));
459 assert(!cir::MissingFeatures::opTBAA());
460
461 Address addr = base.getAddress();
462 if (auto *classDecl = dyn_cast<CXXRecordDecl>(rec)) {
463 if (cgm.getCodeGenOpts().StrictVTablePointers &&
464 classDecl->isDynamicClass()) {
465 cgm.errorNYI(field->getSourceRange(),
466 "emitLValueForField: strict vtable for dynamic class");
467 }
468 }
469
470 unsigned recordCVR = base.getVRQualifiers();
471
472 llvm::StringRef fieldName = field->getName();
473 unsigned fieldIndex;
474 if (cgm.lambdaFieldToName.count(field))
475 fieldName = cgm.lambdaFieldToName[field];
476
477 if (rec->isUnion())
478 fieldIndex = field->getFieldIndex();
479 else {
480 const CIRGenRecordLayout &layout =
481 cgm.getTypes().getCIRGenRecordLayout(field->getParent());
482 fieldIndex = layout.getCIRFieldNo(field);
483 }
484
485 addr = emitAddrOfFieldStorage(addr, field, fieldName, fieldIndex);
486 assert(!cir::MissingFeatures::preservedAccessIndexRegion());
487
488 // If this is a reference field, load the reference right now.
489 if (fieldType->isReferenceType()) {
490 assert(!cir::MissingFeatures::opTBAA());
491 LValue refLVal = makeAddrLValue(addr, fieldType, fieldBaseInfo);
492 if (recordCVR & Qualifiers::Volatile)
493 refLVal.getQuals().addVolatile();
494 addr = emitLoadOfReference(refLVal, getLoc(field->getSourceRange()),
495 &fieldBaseInfo);
496
497 // Qualifiers on the struct don't apply to the referencee.
498 recordCVR = 0;
499 fieldType = fieldType->getPointeeType();
500 }
501
502 if (field->hasAttr<AnnotateAttr>()) {
503 cgm.errorNYI(field->getSourceRange(), "emitLValueForField: AnnotateAttr");
504 return LValue();
505 }
506
507 LValue lv = makeAddrLValue(addr, fieldType, fieldBaseInfo);
508 lv.getQuals().addCVRQualifiers(recordCVR);
509
510 // __weak attribute on a field is ignored.
511 if (lv.getQuals().getObjCGCAttr() == Qualifiers::Weak) {
512 cgm.errorNYI(field->getSourceRange(),
513 "emitLValueForField: __weak attribute");
514 return LValue();
515 }
516
517 return lv;
518 }
519
520 LValue CIRGenFunction::emitLValueForFieldInitialization(
521 LValue base, const clang::FieldDecl *field, llvm::StringRef fieldName) {
522 QualType fieldType = field->getType();
523
524 if (!fieldType->isReferenceType())
525 return emitLValueForField(base, field);
526
527 const CIRGenRecordLayout &layout =
528 cgm.getTypes().getCIRGenRecordLayout(field->getParent());
529 unsigned fieldIndex = layout.getCIRFieldNo(field);
530
531 Address v =
532 emitAddrOfFieldStorage(base.getAddress(), field, fieldName, fieldIndex);
533
534 // Make sure that the address is pointing to the right type.
535 mlir::Type memTy = convertTypeForMem(fieldType);
536 v = builder.createElementBitCast(getLoc(field->getSourceRange()), v, memTy);
537
538 // TODO: Generate TBAA information that describes this access as a structure
539 // member access and not just an access to an object of the field's type. This
540 // should be similar to what we do in EmitLValueForField().
541 LValueBaseInfo baseInfo = base.getBaseInfo();
542 AlignmentSource fieldAlignSource = baseInfo.getAlignmentSource();
543 LValueBaseInfo fieldBaseInfo(getFieldAlignmentSource(fieldAlignSource));
544 assert(!cir::MissingFeatures::opTBAA());
545 return makeAddrLValue(v, fieldType, fieldBaseInfo);
546 }
547
548 mlir::Value CIRGenFunction::emitToMemory(mlir::Value value, QualType ty) {
549 // Bool has a different representation in memory than in registers,
550 // but in ClangIR, it is simply represented as a cir.bool value.
551 // This function is here as a placeholder for possible future changes.
552 return value;
553 }
554
555 void CIRGenFunction::emitStoreOfScalar(mlir::Value value, LValue lvalue,
556 bool isInit) {
557 if (lvalue.getType()->isConstantMatrixType()) {
558 assert(0 && "NYI: emitStoreOfScalar constant matrix type");
559 return;
560 }
561
562 emitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
563 lvalue.getType(), lvalue.getBaseInfo(), isInit,
564 /*isNontemporal=*/false);
565 }
566
567 mlir::Value CIRGenFunction::emitLoadOfScalar(Address addr, bool isVolatile,
568 QualType ty, SourceLocation loc,
569 LValueBaseInfo baseInfo) {
570 // Traditional LLVM codegen handles thread local separately, CIR handles
571 // as part of getAddrOfGlobalVar (GetGlobalOp).
572 mlir::Type eltTy = addr.getElementType();
573
574 if (const auto *clangVecTy = ty->getAs<clang::VectorType>()) {
575 if (clangVecTy->isExtVectorBoolType()) {
576 cgm.errorNYI(loc, "emitLoadOfScalar: ExtVectorBoolType");
577 return nullptr;
578 }
579
580 const auto vecTy = cast<cir::VectorType>(eltTy);
581
582 // Handle vectors of size 3 like size 4 for better performance.
583 assert(!cir::MissingFeatures::cirgenABIInfo());
584 if (vecTy.getSize() == 3 && !getLangOpts().PreserveVec3Type)
585 cgm.errorNYI(addr.getPointer().getLoc(),
586 "emitLoadOfScalar Vec3 & PreserveVec3Type disabled");
587 }
588
589 assert(!cir::MissingFeatures::opLoadStoreTbaa());
590 LValue atomicLValue = LValue::makeAddr(addr, ty, baseInfo);
591 if (ty->isAtomicType() || isLValueSuitableForInlineAtomic(atomicLValue))
592 cgm.errorNYI("emitLoadOfScalar: load atomic");
593
594 if (mlir::isa<cir::VoidType>(eltTy))
595 cgm.errorNYI(loc, "emitLoadOfScalar: void type");
596
597 assert(!cir::MissingFeatures::opLoadEmitScalarRangeCheck());
598
599 mlir::Value loadOp = builder.createLoad(getLoc(loc), addr, isVolatile);
600 if (!ty->isBooleanType() && ty->hasBooleanRepresentation())
601 cgm.errorNYI("emitLoadOfScalar: boolean type with boolean representation");
602
603 return loadOp;
604 }
605
606 mlir::Value CIRGenFunction::emitLoadOfScalar(LValue lvalue,
607 SourceLocation loc) {
608 assert(!cir::MissingFeatures::opLoadStoreNontemporal());
609 assert(!cir::MissingFeatures::opLoadStoreTbaa());
610 return emitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
611 lvalue.getType(), loc, lvalue.getBaseInfo());
612 }
613
614 /// Given an expression that represents a value lvalue, this
615 /// method emits the address of the lvalue, then loads the result as an rvalue,
616 /// returning the rvalue.
617 RValue CIRGenFunction::emitLoadOfLValue(LValue lv, SourceLocation loc) {
618 assert(!lv.getType()->isFunctionType());
619 assert(!(lv.getType()->isConstantMatrixType()) && "not implemented");
620
621 if (lv.isBitField())
622 return emitLoadOfBitfieldLValue(lv, loc);
623
624 if (lv.isSimple())
625 return RValue::get(emitLoadOfScalar(lv, loc));
626
627 if (lv.isVectorElt()) {
628 const mlir::Value load =
629 builder.createLoad(getLoc(loc), lv.getVectorAddress());
630 return RValue::get(cir::VecExtractOp::create(builder, getLoc(loc), load,
631 lv.getVectorIdx()));
632 }
633
634 if (lv.isExtVectorElt())
635 return emitLoadOfExtVectorElementLValue(lv);
636
637 cgm.errorNYI(loc, "emitLoadOfLValue");
638 return RValue::get(nullptr);
639 }
640
641 int64_t CIRGenFunction::getAccessedFieldNo(unsigned int idx,
642 const mlir::ArrayAttr elts) {
643 auto elt = mlir::cast<mlir::IntegerAttr>(elts[idx]);
644 return elt.getInt();
645 }
646
647 // If this is a reference to a subset of the elements of a vector, create an
648 // appropriate shufflevector.
649 RValue CIRGenFunction::emitLoadOfExtVectorElementLValue(LValue lv) {
650 mlir::Location loc = lv.getExtVectorPointer().getLoc();
651 mlir::Value vec = builder.createLoad(loc, lv.getExtVectorAddress());
652
653 // HLSL allows treating scalars as one-element vectors. Converting the scalar
654 // IR value to a vector here allows the rest of codegen to behave as normal.
655 if (getLangOpts().HLSL && !mlir::isa<cir::VectorType>(vec.getType())) {
656 cgm.errorNYI(loc, "emitLoadOfExtVectorElementLValue: HLSL");
657 return {};
658 }
659
660 const mlir::ArrayAttr elts = lv.getExtVectorElts();
661
662 // If the result of the expression is a non-vector type, we must be extracting
663 // a single element. Just codegen as an extractelement.
664 const auto *exprVecTy = lv.getType()->getAs<clang::VectorType>();
665 if (!exprVecTy) {
666 int64_t indexValue = getAccessedFieldNo(0, elts);
667 cir::ConstantOp index =
668 builder.getConstInt(loc, builder.getSInt64Ty(), indexValue);
669 return RValue::get(cir::VecExtractOp::create(builder, loc, vec, index));
670 }
671
672 // Always use shuffle vector to try to retain the original program structure
673 SmallVector<int64_t> mask;
674 for (auto i : llvm::seq<unsigned>(0, exprVecTy->getNumElements()))
675 mask.push_back(getAccessedFieldNo(i, elts));
676
677 cir::VecShuffleOp resultVec = builder.createVecShuffle(loc, vec, mask);
678 if (lv.getType()->isExtVectorBoolType()) {
679 cgm.errorNYI(loc, "emitLoadOfExtVectorElementLValue: ExtVectorBoolType");
680 return {};
681 }
682
683 return RValue::get(resultVec);
684 }
685
686 LValue
687 CIRGenFunction::emitPointerToDataMemberBinaryExpr(const BinaryOperator *e) {
688 assert((e->getOpcode() == BO_PtrMemD || e->getOpcode() == BO_PtrMemI) &&
689 "unexpected binary operator opcode");
690
691 Address baseAddr = Address::invalid();
692 if (e->getOpcode() == BO_PtrMemD)
693 baseAddr = emitLValue(e->getLHS()).getAddress();
694 else
695 baseAddr = emitPointerWithAlignment(e->getLHS());
696
697 const auto *memberPtrTy = e->getRHS()->getType()->castAs<MemberPointerType>();
698
699 mlir::Value memberPtr = emitScalarExpr(e->getRHS());
700
701 LValueBaseInfo baseInfo;
702 assert(!cir::MissingFeatures::opTBAA());
703 Address memberAddr = emitCXXMemberDataPointerAddress(e, baseAddr, memberPtr,
704 memberPtrTy, &baseInfo);
705
706 return makeAddrLValue(memberAddr, memberPtrTy->getPointeeType(), baseInfo);
707 }
708
709 /// Generates lvalue for partial ext_vector access.
710 Address CIRGenFunction::emitExtVectorElementLValue(LValue lv,
711 mlir::Location loc) {
712 Address vectorAddress = lv.getExtVectorAddress();
713 QualType elementTy = lv.getType()->castAs<VectorType>()->getElementType();
714 mlir::Type vectorElementTy = cgm.getTypes().convertType(elementTy);
715 Address castToPointerElement =
716 vectorAddress.withElementType(builder, vectorElementTy);
717
718 mlir::ArrayAttr extVecElts = lv.getExtVectorElts();
719 unsigned idx = getAccessedFieldNo(0, extVecElts);
720 mlir::Value idxValue =
721 builder.getConstInt(loc, mlir::cast<cir::IntType>(ptrDiffTy), idx);
722
723 mlir::Value elementValue = builder.getArrayElement(
724 loc, loc, castToPointerElement.getPointer(), vectorElementTy, idxValue,
725 /*shouldDecay=*/false);
726
727 const CharUnits eltSize = getContext().getTypeSizeInChars(elementTy);
728 const CharUnits alignment =
729 castToPointerElement.getAlignment().alignmentAtOffset(idx * eltSize);
730 return Address(elementValue, vectorElementTy, alignment);
731 }
732
733 static cir::FuncOp emitFunctionDeclPointer(CIRGenModule &cgm, GlobalDecl gd) {
734 assert(!cir::MissingFeatures::weakRefReference());
735 return cgm.getAddrOfFunction(gd);
736 }
737
738 static LValue emitCapturedFieldLValue(CIRGenFunction &cgf, const FieldDecl *fd,
739 mlir::Value thisValue) {
740 return cgf.emitLValueForLambdaField(fd, thisValue);
741 }
742
743 /// Given that we are currently emitting a lambda, emit an l-value for
744 /// one of its members.
745 ///
746 LValue CIRGenFunction::emitLValueForLambdaField(const FieldDecl *field,
747 mlir::Value thisValue) {
748 bool hasExplicitObjectParameter = false;
749 const auto *methD = dyn_cast_if_present<CXXMethodDecl>(curCodeDecl);
750 LValue lambdaLV;
751 if (methD) {
752 hasExplicitObjectParameter = methD->isExplicitObjectMemberFunction();
753 assert(methD->getParent()->isLambda());
754 assert(methD->getParent() == field->getParent());
755 }
756 if (hasExplicitObjectParameter) {
757 cgm.errorNYI(field->getSourceRange(), "ExplicitObjectMemberFunction");
758 } else {
759 QualType lambdaTagType =
760 getContext().getCanonicalTagType(field->getParent());
761 lambdaLV = makeNaturalAlignAddrLValue(thisValue, lambdaTagType);
762 }
763 return emitLValueForField(lambdaLV, field);
764 }
765
766 LValue CIRGenFunction::emitLValueForLambdaField(const FieldDecl *field) {
767 return emitLValueForLambdaField(field, cxxabiThisValue);
768 }
769
770 static LValue emitFunctionDeclLValue(CIRGenFunction &cgf, const Expr *e,
771 GlobalDecl gd) {
772 const FunctionDecl *fd = cast<FunctionDecl>(gd.getDecl());
773 cir::FuncOp funcOp = emitFunctionDeclPointer(cgf.cgm, gd);
774 mlir::Location loc = cgf.getLoc(e->getSourceRange());
775 CharUnits align = cgf.getContext().getDeclAlign(fd);
776
777 assert(!cir::MissingFeatures::sanitizers());
778
779 mlir::Type fnTy = funcOp.getFunctionType();
780 mlir::Type ptrTy = cir::PointerType::get(fnTy);
781 mlir::Value addr = cir::GetGlobalOp::create(cgf.getBuilder(), loc, ptrTy,
782 funcOp.getSymName());
783
784 if (funcOp.getFunctionType() != cgf.convertType(fd->getType())) {
785 fnTy = cgf.convertType(fd->getType());
786 ptrTy = cir::PointerType::get(fnTy);
787
788 addr = cir::CastOp::create(cgf.getBuilder(), addr.getLoc(), ptrTy,
789 cir::CastKind::bitcast, addr);
790 }
791
792 return cgf.makeAddrLValue(Address(addr, fnTy, align), e->getType(),
793 AlignmentSource::Decl);
794 }
795
796 /// Determine whether we can emit a reference to \p vd from the current
797 /// context, despite not necessarily having seen an odr-use of the variable in
798 /// this context.
799 /// TODO(cir): This could be shared with classic codegen.
800 static bool canEmitSpuriousReferenceToVariable(CIRGenFunction &cgf,
801 const DeclRefExpr *e,
802 const VarDecl *vd) {
803 // For a variable declared in an enclosing scope, do not emit a spurious
804 // reference even if we have a capture, as that will emit an unwarranted
805 // reference to our capture state, and will likely generate worse code than
806 // emitting a local copy.
807 if (e->refersToEnclosingVariableOrCapture())
808 return false;
809
810 // For a local declaration declared in this function, we can always reference
811 // it even if we don't have an odr-use.
812 if (vd->hasLocalStorage()) {
813 return vd->getDeclContext() ==
814 dyn_cast_or_null<DeclContext>(cgf.curCodeDecl);
815 }
816
817 // For a global declaration, we can emit a reference to it if we know
818 // for sure that we are able to emit a definition of it.
819 vd = vd->getDefinition(cgf.getContext());
820 if (!vd)
821 return false;
822
823 // Don't emit a spurious reference if it might be to a variable that only
824 // exists on a different device / target.
825 // FIXME: This is unnecessarily broad. Check whether this would actually be a
826 // cross-target reference.
827 if (cgf.getLangOpts().OpenMP || cgf.getLangOpts().CUDA ||
828 cgf.getLangOpts().OpenCL) {
829 return false;
830 }
831
832 // We can emit a spurious reference only if the linkage implies that we'll
833 // be emitting a non-interposable symbol that will be retained until link
834 // time.
835 switch (cgf.cgm.getCIRLinkageVarDefinition(vd, /*IsConstant=*/false)) {
836 case cir::GlobalLinkageKind::ExternalLinkage:
837 case cir::GlobalLinkageKind::LinkOnceODRLinkage:
838 case cir::GlobalLinkageKind::WeakODRLinkage:
839 case cir::GlobalLinkageKind::InternalLinkage:
840 case cir::GlobalLinkageKind::PrivateLinkage:
841 return true;
842 default:
843 return false;
844 }
845 }
846
847 LValue CIRGenFunction::emitDeclRefLValue(const DeclRefExpr *e) {
848 const NamedDecl *nd = e->getDecl();
849 QualType ty = e->getType();
850
851 assert(e->isNonOdrUse() != NOUR_Unevaluated &&
852 "should not emit an unevaluated operand");
853
854 if (const auto *vd = dyn_cast<VarDecl>(nd)) {
855 // Global Named registers access via intrinsics only
856 if (vd->getStorageClass() == SC_Register && vd->hasAttr<AsmLabelAttr>() &&
857 !vd->isLocalVarDecl()) {
858 cgm.errorNYI(e->getSourceRange(),
859 "emitDeclRefLValue: Global Named registers access");
860 return LValue();
861 }
862
863 if (e->isNonOdrUse() == NOUR_Constant &&
864 (vd->getType()->isReferenceType() ||
865 !canEmitSpuriousReferenceToVariable(*this, e, vd))) {
866 cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: NonOdrUse");
867 return LValue();
868 }
869
870 // Check for captured variables.
871 if (e->refersToEnclosingVariableOrCapture()) {
872 vd = vd->getCanonicalDecl();
873 if (FieldDecl *fd = lambdaCaptureFields.lookup(vd))
874 return emitCapturedFieldLValue(*this, fd, cxxabiThisValue);
875 assert(!cir::MissingFeatures::cgCapturedStmtInfo());
876 assert(!cir::MissingFeatures::openMP());
877 }
878 }
879
880 if (const auto *vd = dyn_cast<VarDecl>(nd)) {
881 // Checks for omitted feature handling
882 assert(!cir::MissingFeatures::opAllocaStaticLocal());
883 assert(!cir::MissingFeatures::opAllocaNonGC());
884 assert(!cir::MissingFeatures::opAllocaImpreciseLifetime());
885 assert(!cir::MissingFeatures::opAllocaTLS());
886 assert(!cir::MissingFeatures::opAllocaOpenMPThreadPrivate());
887 assert(!cir::MissingFeatures::opAllocaEscapeByReference());
888
889 // Check if this is a global variable
890 if (vd->hasLinkage() || vd->isStaticDataMember())
891 return emitGlobalVarDeclLValue(*this, e, vd);
892
893 Address addr = Address::invalid();
894
895 // The variable should generally be present in the local decl map.
896 auto iter = localDeclMap.find(vd);
897 if (iter != localDeclMap.end()) {
898 addr = iter->second;
899 } else {
900 // Otherwise, it might be static local we haven't emitted yet for some
901 // reason; most likely, because it's in an outer function.
902 cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: static local");
903 }
904
905 // Drill into reference types.
906 LValue lv =
907 vd->getType()->isReferenceType()
908 ? emitLoadOfReferenceLValue(addr, getLoc(e->getSourceRange()),
909 vd->getType(), AlignmentSource::Decl)
910 : makeAddrLValue(addr, ty, AlignmentSource::Decl);
911
912 // Statics are defined as globals, so they are not include in the function's
913 // symbol table.
914 assert((vd->isStaticLocal() || symbolTable.count(vd)) &&
915 "non-static locals should be already mapped");
916
917 return lv;
918 }
919
920 if (const auto *bd = dyn_cast<BindingDecl>(nd)) {
921 if (e->refersToEnclosingVariableOrCapture()) {
922 assert(!cir::MissingFeatures::lambdaCaptures());
923 cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: lambda captures");
924 return LValue();
925 }
926 return emitLValue(bd->getBinding());
927 }
928
929 if (const auto *fd = dyn_cast<FunctionDecl>(nd)) {
930 LValue lv = emitFunctionDeclLValue(*this, e, fd);
931
932 // Emit debuginfo for the function declaration if the target wants to.
933 if (getContext().getTargetInfo().allowDebugInfoForExternalRef())
934 assert(!cir::MissingFeatures::generateDebugInfo());
935
936 return lv;
937 }
938
939 cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: unhandled decl type");
940 return LValue();
941 }
942
943 mlir::Value CIRGenFunction::evaluateExprAsBool(const Expr *e) {
944 QualType boolTy = getContext().BoolTy;
945 SourceLocation loc = e->getExprLoc();
946
947 assert(!cir::MissingFeatures::pgoUse());
948 if (e->getType()->getAs<MemberPointerType>()) {
949 cgm.errorNYI(e->getSourceRange(),
950 "evaluateExprAsBool: member pointer type");
951 return createDummyValue(getLoc(loc), boolTy);
952 }
953
954 assert(!cir::MissingFeatures::cgFPOptionsRAII());
955 if (!e->getType()->isAnyComplexType())
956 return emitScalarConversion(emitScalarExpr(e), e->getType(), boolTy, loc);
957
958 return emitComplexToScalarConversion(emitComplexExpr(e), e->getType(), boolTy,
959 loc);
960 }
961
962 LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *e) {
963 UnaryOperatorKind op = e->getOpcode();
964
965 // __extension__ doesn't affect lvalue-ness.
966 if (op == UO_Extension)
967 return emitLValue(e->getSubExpr());
968
969 switch (op) {
970 case UO_Deref: {
971 QualType t = e->getSubExpr()->getType()->getPointeeType();
972 assert(!t.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
973
974 assert(!cir::MissingFeatures::opTBAA());
975 LValueBaseInfo baseInfo;
976 Address addr = emitPointerWithAlignment(e->getSubExpr(), &baseInfo);
977
978 // Tag 'load' with deref attribute.
979 // FIXME: This misses some derefence cases and has problematic interactions
980 // with other operators.
981 if (auto loadOp = addr.getDefiningOp<cir::LoadOp>())
982 loadOp.setIsDerefAttr(mlir::UnitAttr::get(&getMLIRContext()));
983
984 LValue lv = makeAddrLValue(addr, t, baseInfo);
985 assert(!cir::MissingFeatures::addressSpace());
986 assert(!cir::MissingFeatures::setNonGC());
987 return lv;
988 }
989 case UO_Real:
990 case UO_Imag: {
991 LValue lv = emitLValue(e->getSubExpr());
992 assert(lv.isSimple() && "real/imag on non-ordinary l-value");
993
994 // __real is valid on scalars. This is a faster way of testing that.
995 // __imag can only produce an rvalue on scalars.
996 if (e->getOpcode() == UO_Real &&
997 !mlir::isa<cir::ComplexType>(lv.getAddress().getElementType())) {
998 assert(e->getSubExpr()->getType()->isArithmeticType());
999 return lv;
1000 }
1001
1002 QualType exprTy = getContext().getCanonicalType(e->getSubExpr()->getType());
1003 QualType elemTy = exprTy->castAs<clang::ComplexType>()->getElementType();
1004 mlir::Location loc = getLoc(e->getExprLoc());
1005 Address component =
1006 e->getOpcode() == UO_Real
1007 ? builder.createComplexRealPtr(loc, lv.getAddress())
1008 : builder.createComplexImagPtr(loc, lv.getAddress());
1009 assert(!cir::MissingFeatures::opTBAA());
1010 LValue elemLV = makeAddrLValue(component, elemTy);
1011 elemLV.getQuals().addQualifiers(lv.getQuals());
1012 return elemLV;
1013 }
1014 case UO_PreInc:
1015 case UO_PreDec: {
1016 cir::UnaryOpKind kind =
1017 e->isIncrementOp() ? cir::UnaryOpKind::Inc : cir::UnaryOpKind::Dec;
1018 LValue lv = emitLValue(e->getSubExpr());
1019
1020 assert(e->isPrefix() && "Prefix operator in unexpected state!");
1021
1022 if (e->getType()->isAnyComplexType()) {
1023 emitComplexPrePostIncDec(e, lv, kind, /*isPre=*/true);
1024 } else {
1025 emitScalarPrePostIncDec(e, lv, kind, /*isPre=*/true);
1026 }
1027
1028 return lv;
1029 }
1030 case UO_Extension:
1031 llvm_unreachable("UnaryOperator extension should be handled above!");
1032 case UO_Plus:
1033 case UO_Minus:
1034 case UO_Not:
1035 case UO_LNot:
1036 case UO_AddrOf:
1037 case UO_PostInc:
1038 case UO_PostDec:
1039 case UO_Coawait:
1040 llvm_unreachable("UnaryOperator of non-lvalue kind!");
1041 }
1042 llvm_unreachable("Unknown unary operator kind!");
1043 }
1044
1045 /// If the specified expr is a simple decay from an array to pointer,
1046 /// return the array subexpression.
1047 /// FIXME: this could be abstracted into a common AST helper.
1048 static const Expr *getSimpleArrayDecayOperand(const Expr *e) {
1049 // If this isn't just an array->pointer decay, bail out.
1050 const auto *castExpr = dyn_cast<CastExpr>(e);
1051 if (!castExpr || castExpr->getCastKind() != CK_ArrayToPointerDecay)
1052 return nullptr;
1053
1054 // If this is a decay from variable width array, bail out.
1055 const Expr *subExpr = castExpr->getSubExpr();
1056 if (subExpr->getType()->isVariableArrayType())
1057 return nullptr;
1058
1059 return subExpr;
1060 }
1061
1062 static cir::IntAttr getConstantIndexOrNull(mlir::Value idx) {
1063 // TODO(cir): should we consider using MLIRs IndexType instead of IntegerAttr?
1064 if (auto constantOp = idx.getDefiningOp<cir::ConstantOp>())
1065 return constantOp.getValueAttr<cir::IntAttr>();
1066 return {};
1067 }
1068
1069 static CharUnits getArrayElementAlign(CharUnits arrayAlign, mlir::Value idx,
1070 CharUnits eltSize) {
1071 // If we have a constant index, we can use the exact offset of the
1072 // element we're accessing.
1073 if (const cir::IntAttr constantIdx = getConstantIndexOrNull(idx)) {
1074 const CharUnits offset = constantIdx.getValue().getZExtValue() * eltSize;
1075 return arrayAlign.alignmentAtOffset(offset);
1076 }
1077 // Otherwise, use the worst-case alignment for any element.
1078 return arrayAlign.alignmentOfArrayElement(eltSize);
1079 }
1080
1081 static QualType getFixedSizeElementType(const ASTContext &astContext,
1082 const VariableArrayType *vla) {
1083 QualType eltType;
1084 do {
1085 eltType = vla->getElementType();
1086 } while ((vla = astContext.getAsVariableArrayType(eltType)));
1087 return eltType;
1088 }
1089
1090 static mlir::Value emitArraySubscriptPtr(CIRGenFunction &cgf,
1091 mlir::Location beginLoc,
1092 mlir::Location endLoc, mlir::Value ptr,
1093 mlir::Type eltTy, mlir::Value idx,
1094 bool shouldDecay) {
1095 CIRGenModule &cgm = cgf.getCIRGenModule();
1096 // TODO(cir): LLVM codegen emits in bound gep check here, is there anything
1097 // that would enhance tracking this later in CIR?
1098 assert(!cir::MissingFeatures::emitCheckedInBoundsGEP());
1099 return cgm.getBuilder().getArrayElement(beginLoc, endLoc, ptr, eltTy, idx,
1100 shouldDecay);
1101 }
1102
1103 static Address emitArraySubscriptPtr(CIRGenFunction &cgf,
1104 mlir::Location beginLoc,
1105 mlir::Location endLoc, Address addr,
1106 QualType eltType, mlir::Value idx,
1107 mlir::Location loc, bool shouldDecay) {
1108
1109 // Determine the element size of the statically-sized base. This is
1110 // the thing that the indices are expressed in terms of.
1111 if (const VariableArrayType *vla =
1112 cgf.getContext().getAsVariableArrayType(eltType)) {
1113 eltType = getFixedSizeElementType(cgf.getContext(), vla);
1114 }
1115
1116 // We can use that to compute the best alignment of the element.
1117 const CharUnits eltSize = cgf.getContext().getTypeSizeInChars(eltType);
1118 const CharUnits eltAlign =
1119 getArrayElementAlign(addr.getAlignment(), idx, eltSize);
1120
1121 assert(!cir::MissingFeatures::preservedAccessIndexRegion());
1122 const mlir::Value eltPtr =
1123 emitArraySubscriptPtr(cgf, beginLoc, endLoc, addr.getPointer(),
1124 addr.getElementType(), idx, shouldDecay);
1125 const mlir::Type elementType = cgf.convertTypeForMem(eltType);
1126 return Address(eltPtr, elementType, eltAlign);
1127 }
1128
1129 LValue
1130 CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) {
1131 if (getContext().getAsVariableArrayType(e->getType())) {
1132 cgm.errorNYI(e->getSourceRange(),
1133 "emitArraySubscriptExpr: VariableArrayType");
1134 return LValue::makeAddr(Address::invalid(), e->getType(), LValueBaseInfo());
1135 }
1136
1137 if (e->getType()->getAs<ObjCObjectType>()) {
1138 cgm.errorNYI(e->getSourceRange(), "emitArraySubscriptExpr: ObjCObjectType");
1139 return LValue::makeAddr(Address::invalid(), e->getType(), LValueBaseInfo());
1140 }
1141
1142 // The index must always be an integer, which is not an aggregate. Emit it
1143 // in lexical order (this complexity is, sadly, required by C++17).
1144 assert((e->getIdx() == e->getLHS() || e->getIdx() == e->getRHS()) &&
1145 "index was neither LHS nor RHS");
1146
1147 auto emitIdxAfterBase = [&](bool promote) -> mlir::Value {
1148 const mlir::Value idx = emitScalarExpr(e->getIdx());
1149
1150 // Extend or truncate the index type to 32 or 64-bits.
1151 auto ptrTy = mlir::dyn_cast<cir::PointerType>(idx.getType());
1152 if (promote && ptrTy && ptrTy.isPtrTo<cir::IntType>())
1153 cgm.errorNYI(e->getSourceRange(),
1154 "emitArraySubscriptExpr: index type cast");
1155 return idx;
1156 };
1157
1158 // If the base is a vector type, then we are forming a vector element
1159 // with this subscript.
1160 if (e->getBase()->getType()->isSubscriptableVectorType() &&
1161 !isa<ExtVectorElementExpr>(e->getBase())) {
1162 const mlir::Value idx = emitIdxAfterBase(/*promote=*/false);
1163 const LValue lv = emitLValue(e->getBase());
1164 return LValue::makeVectorElt(lv.getAddress(), idx, e->getBase()->getType(),
1165 lv.getBaseInfo());
1166 }
1167
1168 const mlir::Value idx = emitIdxAfterBase(/*promote=*/true);
1169
1170 // Handle the extvector case we ignored above.
1171 if (isa<ExtVectorElementExpr>(e->getBase())) {
1172 const LValue lv = emitLValue(e->getBase());
1173 Address addr = emitExtVectorElementLValue(lv, cgm.getLoc(e->getExprLoc()));
1174
1175 QualType elementType = lv.getType()->castAs<VectorType>()->getElementType();
1176 addr = emitArraySubscriptPtr(*this, cgm.getLoc(e->getBeginLoc()),
1177 cgm.getLoc(e->getEndLoc()), addr, e->getType(),
1178 idx, cgm.getLoc(e->getExprLoc()),
1179 /*shouldDecay=*/false);
1180
1181 return makeAddrLValue(addr, elementType, lv.getBaseInfo());
1182 }
1183
1184 if (const Expr *array = getSimpleArrayDecayOperand(e->getBase())) {
1185 LValue arrayLV;
1186 if (const auto *ase = dyn_cast<ArraySubscriptExpr>(array))
1187 arrayLV = emitArraySubscriptExpr(ase);
1188 else
1189 arrayLV = emitLValue(array);
1190
1191 // Propagate the alignment from the array itself to the result.
1192 const Address addr = emitArraySubscriptPtr(
1193 *this, cgm.getLoc(array->getBeginLoc()), cgm.getLoc(array->getEndLoc()),
1194 arrayLV.getAddress(), e->getType(), idx, cgm.getLoc(e->getExprLoc()),
1195 /*shouldDecay=*/true);
1196
1197 const LValue lv = LValue::makeAddr(addr, e->getType(), LValueBaseInfo());
1198
1199 if (getLangOpts().ObjC && getLangOpts().getGC() != LangOptions::NonGC) {
1200 cgm.errorNYI(e->getSourceRange(), "emitArraySubscriptExpr: ObjC with GC");
1201 }
1202
1203 return lv;
1204 }
1205
1206 // The base must be a pointer; emit it with an estimate of its alignment.
1207 assert(e->getBase()->getType()->isPointerType() &&
1208 "The base must be a pointer");
1209
1210 LValueBaseInfo eltBaseInfo;
1211 const Address ptrAddr = emitPointerWithAlignment(e->getBase(), &eltBaseInfo);
1212 // Propagate the alignment from the array itself to the result.
1213 const Address addxr = emitArraySubscriptPtr(
1214 *this, cgm.getLoc(e->getBeginLoc()), cgm.getLoc(e->getEndLoc()), ptrAddr,
1215 e->getType(), idx, cgm.getLoc(e->getExprLoc()),
1216 /*shouldDecay=*/false);
1217
1218 const LValue lv = LValue::makeAddr(addxr, e->getType(), eltBaseInfo);
1219
1220 if (getLangOpts().ObjC && getLangOpts().getGC() != LangOptions::NonGC) {
1221 cgm.errorNYI(e->getSourceRange(), "emitArraySubscriptExpr: ObjC with GC");
1222 }
1223
1224 return lv;
1225 }
1226
1227 LValue CIRGenFunction::emitExtVectorElementExpr(const ExtVectorElementExpr *e) {
1228 // Emit the base vector as an l-value.
1229 LValue base;
1230
1231 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
1232 if (e->isArrow()) {
1233 // If it is a pointer to a vector, emit the address and form an lvalue with
1234 // it.
1235 LValueBaseInfo baseInfo;
1236 Address ptr = emitPointerWithAlignment(e->getBase(), &baseInfo);
1237 const auto *clangPtrTy =
1238 e->getBase()->getType()->castAs<clang::PointerType>();
1239 base = makeAddrLValue(ptr, clangPtrTy->getPointeeType(), baseInfo);
1240 base.getQuals().removeObjCGCAttr();
1241 } else if (e->getBase()->isGLValue()) {
1242 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1243 // emit the base as an lvalue.
1244 assert(e->getBase()->getType()->isVectorType());
1245 base = emitLValue(e->getBase());
1246 } else {
1247 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
1248 assert(e->getBase()->getType()->isVectorType() &&
1249 "Result must be a vector");
1250 mlir::Value vec = emitScalarExpr(e->getBase());
1251
1252 // Store the vector to memory (because LValue wants an address).
1253 QualType baseTy = e->getBase()->getType();
1254 Address vecMem = createMemTemp(baseTy, vec.getLoc(), "tmp");
1255 if (!getLangOpts().HLSL && baseTy->isExtVectorBoolType()) {
1256 cgm.errorNYI(e->getSourceRange(),
1257 "emitExtVectorElementExpr: ExtVectorBoolType & !HLSL");
1258 return {};
1259 }
1260 builder.createStore(vec.getLoc(), vec, vecMem);
1261 base = makeAddrLValue(vecMem, baseTy, AlignmentSource::Decl);
1262 }
1263
1264 QualType type =
1265 e->getType().withCVRQualifiers(base.getQuals().getCVRQualifiers());
1266
1267 // Encode the element access list into a vector of unsigned indices.
1268 SmallVector<uint32_t, 4> indices;
1269 e->getEncodedElementAccess(indices);
1270
1271 if (base.isSimple()) {
1272 SmallVector<int64_t> attrElts(indices.begin(), indices.end());
1273 mlir::ArrayAttr elts = builder.getI64ArrayAttr(attrElts);
1274 return LValue::makeExtVectorElt(base.getAddress(), elts, type,
1275 base.getBaseInfo());
1276 }
1277
1278 cgm.errorNYI(e->getSourceRange(),
1279 "emitExtVectorElementExpr: isSimple is false");
1280 return {};
1281 }
1282
1283 LValue CIRGenFunction::emitStringLiteralLValue(const StringLiteral *e,
1284 llvm::StringRef name) {
1285 cir::GlobalOp globalOp = cgm.getGlobalForStringLiteral(e, name);
1286 assert(globalOp.getAlignment() && "expected alignment for string literal");
1287 unsigned align = *(globalOp.getAlignment());
1288 mlir::Value addr =
1289 builder.createGetGlobal(getLoc(e->getSourceRange()), globalOp);
1290 return makeAddrLValue(
1291 Address(addr, globalOp.getSymType(), CharUnits::fromQuantity(align)),
1292 e->getType(), AlignmentSource::Decl);
1293 }
1294
1295 /// Casts are never lvalues unless that cast is to a reference type. If the cast
1296 /// is to a reference, we can have the usual lvalue result, otherwise if a cast
1297 /// is needed by the code generator in an lvalue context, then it must mean that
1298 /// we need the address of an aggregate in order to access one of its members.
1299 /// This can happen for all the reasons that casts are permitted with aggregate
1300 /// result, including noop aggregate casts, and cast from scalar to union.
1301 LValue CIRGenFunction::emitCastLValue(const CastExpr *e) {
1302 switch (e->getCastKind()) {
1303 case CK_ToVoid:
1304 case CK_BitCast:
1305 case CK_LValueToRValueBitCast:
1306 case CK_ArrayToPointerDecay:
1307 case CK_FunctionToPointerDecay:
1308 case CK_NullToMemberPointer:
1309 case CK_NullToPointer:
1310 case CK_IntegralToPointer:
1311 case CK_PointerToIntegral:
1312 case CK_PointerToBoolean:
1313 case CK_IntegralCast:
1314 case CK_BooleanToSignedIntegral:
1315 case CK_IntegralToBoolean:
1316 case CK_IntegralToFloating:
1317 case CK_FloatingToIntegral:
1318 case CK_FloatingToBoolean:
1319 case CK_FloatingCast:
1320 case CK_FloatingRealToComplex:
1321 case CK_FloatingComplexToReal:
1322 case CK_FloatingComplexToBoolean:
1323 case CK_FloatingComplexCast:
1324 case CK_FloatingComplexToIntegralComplex:
1325 case CK_IntegralRealToComplex:
1326 case CK_IntegralComplexToReal:
1327 case CK_IntegralComplexToBoolean:
1328 case CK_IntegralComplexCast:
1329 case CK_IntegralComplexToFloatingComplex:
1330 case CK_DerivedToBaseMemberPointer:
1331 case CK_BaseToDerivedMemberPointer:
1332 case CK_MemberPointerToBoolean:
1333 case CK_ReinterpretMemberPointer:
1334 case CK_AnyPointerToBlockPointerCast:
1335 case CK_ARCProduceObject:
1336 case CK_ARCConsumeObject:
1337 case CK_ARCReclaimReturnedObject:
1338 case CK_ARCExtendBlockObject:
1339 case CK_CopyAndAutoreleaseBlockObject:
1340 case CK_IntToOCLSampler:
1341 case CK_FloatingToFixedPoint:
1342 case CK_FixedPointToFloating:
1343 case CK_FixedPointCast:
1344 case CK_FixedPointToBoolean:
1345 case CK_FixedPointToIntegral:
1346 case CK_IntegralToFixedPoint:
1347 case CK_MatrixCast:
1348 case CK_HLSLVectorTruncation:
1349 case CK_HLSLMatrixTruncation:
1350 case CK_HLSLArrayRValue:
1351 case CK_HLSLElementwiseCast:
1352 case CK_HLSLAggregateSplatCast:
1353 llvm_unreachable("unexpected cast lvalue");
1354
1355 case CK_Dependent:
1356 llvm_unreachable("dependent cast kind in IR gen!");
1357
1358 case CK_BuiltinFnToFnPtr:
1359 llvm_unreachable("builtin functions are handled elsewhere");
1360
1361 case CK_Dynamic: {
1362 LValue lv = emitLValue(e->getSubExpr());
1363 Address v = lv.getAddress();
1364 const auto *dce = cast<CXXDynamicCastExpr>(e);
1365 return makeNaturalAlignAddrLValue(emitDynamicCast(v, dce), e->getType());
1366 }
1367
1368 // These are never l-values; just use the aggregate emission code.
1369 case CK_NonAtomicToAtomic:
1370 case CK_AtomicToNonAtomic:
1371 case CK_ToUnion:
1372 case CK_ObjCObjectLValueCast:
1373 case CK_VectorSplat:
1374 case CK_ConstructorConversion:
1375 case CK_UserDefinedConversion:
1376 case CK_CPointerToObjCPointerCast:
1377 case CK_BlockPointerToObjCPointerCast:
1378 case CK_LValueToRValue: {
1379 cgm.errorNYI(e->getSourceRange(),
1380 std::string("emitCastLValue for unhandled cast kind: ") +
1381 e->getCastKindName());
1382
1383 return {};
1384 }
1385 case CK_AddressSpaceConversion: {
1386 LValue lv = emitLValue(e->getSubExpr());
1387 QualType destTy = getContext().getPointerType(e->getType());
1388
1389 clang::LangAS srcLangAS = e->getSubExpr()->getType().getAddressSpace();
1390 cir::TargetAddressSpaceAttr srcAS;
1391 if (clang::isTargetAddressSpace(srcLangAS))
1392 srcAS = cir::toCIRTargetAddressSpace(getMLIRContext(), srcLangAS);
1393 else
1394 cgm.errorNYI(
1395 e->getSourceRange(),
1396 "emitCastLValue: address space conversion from unknown address "
1397 "space");
1398
1399 mlir::Value v = getTargetHooks().performAddrSpaceCast(
1400 *this, lv.getPointer(), srcAS, convertType(destTy));
1401
1402 return makeAddrLValue(Address(v, convertTypeForMem(e->getType()),
1403 lv.getAddress().getAlignment()),
1404 e->getType(), lv.getBaseInfo());
1405 }
1406
1407 case CK_LValueBitCast: {
1408 // This must be a reinterpret_cast (or c-style equivalent).
1409 const auto *ce = cast<ExplicitCastExpr>(e);
1410
1411 cgm.emitExplicitCastExprType(ce, this);
1412 LValue LV = emitLValue(e->getSubExpr());
1413 Address V = LV.getAddress().withElementType(
1414 builder, convertTypeForMem(ce->getTypeAsWritten()->getPointeeType()));
1415
1416 return makeAddrLValue(V, e->getType(), LV.getBaseInfo());
1417 }
1418
1419 case CK_NoOp: {
1420 // CK_NoOp can model a qualification conversion, which can remove an array
1421 // bound and change the IR type.
1422 LValue lv = emitLValue(e->getSubExpr());
1423 // Propagate the volatile qualifier to LValue, if exists in e.
1424 if (e->changesVolatileQualification())
1425 cgm.errorNYI(e->getSourceRange(),
1426 "emitCastLValue: NoOp changes volatile qual");
1427 if (lv.isSimple()) {
1428 Address v = lv.getAddress();
1429 if (v.isValid()) {
1430 mlir::Type ty = convertTypeForMem(e->getType());
1431 if (v.getElementType() != ty)
1432 cgm.errorNYI(e->getSourceRange(),
1433 "emitCastLValue: NoOp needs bitcast");
1434 }
1435 }
1436 return lv;
1437 }
1438
1439 case CK_UncheckedDerivedToBase:
1440 case CK_DerivedToBase: {
1441 auto *derivedClassDecl = e->getSubExpr()->getType()->castAsCXXRecordDecl();
1442
1443 LValue lv = emitLValue(e->getSubExpr());
1444 Address thisAddr = lv.getAddress();
1445
1446 // Perform the derived-to-base conversion
1447 Address baseAddr =
1448 getAddressOfBaseClass(thisAddr, derivedClassDecl, e->path(),
1449 /*NullCheckValue=*/false, e->getExprLoc());
1450
1451 // TODO: Support accesses to members of base classes in TBAA. For now, we
1452 // conservatively pretend that the complete object is of the base class
1453 // type.
1454 assert(!cir::MissingFeatures::opTBAA());
1455 return makeAddrLValue(baseAddr, e->getType(), lv.getBaseInfo());
1456 }
1457
1458 case CK_BaseToDerived: {
1459 const auto *derivedClassDecl = e->getType()->castAsCXXRecordDecl();
1460 LValue lv = emitLValue(e->getSubExpr());
1461
1462 // Perform the base-to-derived conversion
1463 Address derived = getAddressOfDerivedClass(
1464 getLoc(e->getSourceRange()), lv.getAddress(), derivedClassDecl,
1465 e->path(), /*NullCheckValue=*/false);
1466 // C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is
1467 // performed and the object is not of the derived type.
1468 assert(!cir::MissingFeatures::sanitizers());
1469
1470 assert(!cir::MissingFeatures::opTBAA());
1471 return makeAddrLValue(derived, e->getType(), lv.getBaseInfo());
1472 }
1473
1474 case CK_ZeroToOCLOpaqueType:
1475 llvm_unreachable("NULL to OpenCL opaque type lvalue cast is not valid");
1476 }
1477
1478 llvm_unreachable("Invalid cast kind");
1479 }
1480
1481 static DeclRefExpr *tryToConvertMemberExprToDeclRefExpr(CIRGenFunction &cgf,
1482 const MemberExpr *me) {
1483 if (auto *vd = dyn_cast<VarDecl>(me->getMemberDecl())) {
1484 // Try to emit static variable member expressions as DREs.
1485 return DeclRefExpr::Create(
1486 cgf.getContext(), NestedNameSpecifierLoc(), SourceLocation(), vd,
1487 /*RefersToEnclosingVariableOrCapture=*/false, me->getExprLoc(),
1488 me->getType(), me->getValueKind(), nullptr, nullptr, me->isNonOdrUse());
1489 }
1490 return nullptr;
1491 }
1492
1493 LValue CIRGenFunction::emitMemberExpr(const MemberExpr *e) {
1494 if (DeclRefExpr *dre = tryToConvertMemberExprToDeclRefExpr(*this, e)) {
1495 emitIgnoredExpr(e->getBase());
1496 return emitDeclRefLValue(dre);
1497 }
1498
1499 Expr *baseExpr = e->getBase();
1500 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
1501 LValue baseLV;
1502 if (e->isArrow()) {
1503 LValueBaseInfo baseInfo;
1504 assert(!cir::MissingFeatures::opTBAA());
1505 Address addr = emitPointerWithAlignment(baseExpr, &baseInfo);
1506 QualType ptrTy = baseExpr->getType()->getPointeeType();
1507 assert(!cir::MissingFeatures::typeChecks());
1508 baseLV = makeAddrLValue(addr, ptrTy, baseInfo);
1509 } else {
1510 assert(!cir::MissingFeatures::typeChecks());
1511 baseLV = emitLValue(baseExpr);
1512 }
1513
1514 const NamedDecl *nd = e->getMemberDecl();
1515 if (auto *field = dyn_cast<FieldDecl>(nd)) {
1516 LValue lv = emitLValueForField(baseLV, field);
1517 assert(!cir::MissingFeatures::setObjCGCLValueClass());
1518 if (getLangOpts().OpenMP) {
1519 // If the member was explicitly marked as nontemporal, mark it as
1520 // nontemporal. If the base lvalue is marked as nontemporal, mark access
1521 // to children as nontemporal too.
1522 cgm.errorNYI(e->getSourceRange(), "emitMemberExpr: OpenMP");
1523 }
1524 return lv;
1525 }
1526
1527 if (isa<FunctionDecl>(nd)) {
1528 cgm.errorNYI(e->getSourceRange(), "emitMemberExpr: FunctionDecl");
1529 return LValue();
1530 }
1531
1532 llvm_unreachable("Unhandled member declaration!");
1533 }
1534
1535 /// Evaluate an expression into a given memory location.
1536 void CIRGenFunction::emitAnyExprToMem(const Expr *e, Address location,
1537 Qualifiers quals, bool isInit) {
1538 // FIXME: This function should take an LValue as an argument.
1539 switch (getEvaluationKind(e->getType())) {
1540 case cir::TEK_Complex: {
1541 LValue lv = makeAddrLValue(location, e->getType());
1542 emitComplexExprIntoLValue(e, lv, isInit);
1543 return;
1544 }
1545
1546 case cir::TEK_Aggregate: {
1547 emitAggExpr(e, AggValueSlot::forAddr(location, quals,
1548 AggValueSlot::IsDestructed_t(isInit),
1549 AggValueSlot::IsAliased_t(!isInit),
1550 AggValueSlot::MayOverlap));
1551 return;
1552 }
1553
1554 case cir::TEK_Scalar: {
1555 RValue rv = RValue::get(emitScalarExpr(e));
1556 LValue lv = makeAddrLValue(location, e->getType());
1557 emitStoreThroughLValue(rv, lv);
1558 return;
1559 }
1560 }
1561
1562 llvm_unreachable("bad evaluation kind");
1563 }
1564
1565 static Address createReferenceTemporary(CIRGenFunction &cgf,
1566 const MaterializeTemporaryExpr *m,
1567 const Expr *inner) {
1568 // TODO(cir): cgf.getTargetHooks();
1569 switch (m->getStorageDuration()) {
1570 case SD_FullExpression:
1571 case SD_Automatic: {
1572 QualType ty = inner->getType();
1573
1574 assert(!cir::MissingFeatures::mergeAllConstants());
1575
1576 // The temporary memory should be created in the same scope as the extending
1577 // declaration of the temporary materialization expression.
1578 cir::AllocaOp extDeclAlloca;
1579 if (const ValueDecl *extDecl = m->getExtendingDecl()) {
1580 auto extDeclAddrIter = cgf.localDeclMap.find(extDecl);
1581 if (extDeclAddrIter != cgf.localDeclMap.end())
1582 extDeclAlloca = extDeclAddrIter->second.getDefiningOp<cir::AllocaOp>();
1583 }
1584 mlir::OpBuilder::InsertPoint ip;
1585 if (extDeclAlloca)
1586 ip = {extDeclAlloca->getBlock(), extDeclAlloca->getIterator()};
1587 return cgf.createMemTemp(ty, cgf.getLoc(m->getSourceRange()),
1588 cgf.getCounterRefTmpAsString(), /*alloca=*/nullptr,
1589 ip);
1590 }
1591 case SD_Thread:
1592 case SD_Static: {
1593 cgf.cgm.errorNYI(
1594 m->getSourceRange(),
1595 "createReferenceTemporary: static/thread storage duration");
1596 return Address::invalid();
1597 }
1598
1599 case SD_Dynamic:
1600 llvm_unreachable("temporary can't have dynamic storage duration");
1601 }
1602 llvm_unreachable("unknown storage duration");
1603 }
1604
1605 static void pushTemporaryCleanup(CIRGenFunction &cgf,
1606 const MaterializeTemporaryExpr *m,
1607 const Expr *e, Address referenceTemporary) {
1608 // Objective-C++ ARC:
1609 // If we are binding a reference to a temporary that has ownership, we
1610 // need to perform retain/release operations on the temporary.
1611 //
1612 // FIXME(ogcg): This should be looking at e, not m.
1613 if (m->getType().getObjCLifetime()) {
1614 cgf.cgm.errorNYI(e->getSourceRange(), "pushTemporaryCleanup: ObjCLifetime");
1615 return;
1616 }
1617
1618 const QualType::DestructionKind dk = e->getType().isDestructedType();
1619 if (dk == QualType::DK_none)
1620 return;
1621
1622 switch (m->getStorageDuration()) {
1623 case SD_Static:
1624 case SD_Thread: {
1625 CXXDestructorDecl *referenceTemporaryDtor = nullptr;
1626 if (const auto *classDecl =
1627 e->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
1628 classDecl && !classDecl->hasTrivialDestructor())
1629 // Get the destructor for the reference temporary.
1630 referenceTemporaryDtor = classDecl->getDestructor();
1631
1632 if (!referenceTemporaryDtor)
1633 return;
1634
1635 cgf.cgm.errorNYI(e->getSourceRange(), "pushTemporaryCleanup: static/thread "
1636 "storage duration with destructors");
1637 break;
1638 }
1639
1640 case SD_FullExpression:
1641 cgf.pushDestroy(NormalAndEHCleanup, referenceTemporary, e->getType(),
1642 CIRGenFunction::destroyCXXObject);
1643 break;
1644
1645 case SD_Automatic:
1646 cgf.cgm.errorNYI(e->getSourceRange(),
1647 "pushTemporaryCleanup: automatic storage duration");
1648 break;
1649
1650 case SD_Dynamic:
1651 llvm_unreachable("temporary cannot have dynamic storage duration");
1652 }
1653 }
1654
1655 LValue CIRGenFunction::emitMaterializeTemporaryExpr(
1656 const MaterializeTemporaryExpr *m) {
1657 const Expr *e = m->getSubExpr();
1658
1659 assert((!m->getExtendingDecl() || !isa<VarDecl>(m->getExtendingDecl()) ||
1660 !cast<VarDecl>(m->getExtendingDecl())->isARCPseudoStrong()) &&
1661 "Reference should never be pseudo-strong!");
1662
1663 // FIXME: ideally this would use emitAnyExprToMem, however, we cannot do so
1664 // as that will cause the lifetime adjustment to be lost for ARC
1665 auto ownership = m->getType().getObjCLifetime();
1666 if (ownership != Qualifiers::OCL_None &&
1667 ownership != Qualifiers::OCL_ExplicitNone) {
1668 cgm.errorNYI(e->getSourceRange(),
1669 "emitMaterializeTemporaryExpr: ObjCLifetime");
1670 return {};
1671 }
1672
1673 SmallVector<const Expr *, 2> commaLHSs;
1674 SmallVector<SubobjectAdjustment, 2> adjustments;
1675 e = e->skipRValueSubobjectAdjustments(commaLHSs, adjustments);
1676
1677 for (const Expr *ignored : commaLHSs)
1678 emitIgnoredExpr(ignored);
1679
1680 if (isa<OpaqueValueExpr>(e)) {
1681 cgm.errorNYI(e->getSourceRange(),
1682 "emitMaterializeTemporaryExpr: OpaqueValueExpr");
1683 return {};
1684 }
1685
1686 // Create and initialize the reference temporary.
1687 Address object = createReferenceTemporary(*this, m, e);
1688
1689 if (auto var = object.getPointer().getDefiningOp<cir::GlobalOp>()) {
1690 // TODO(cir): add something akin to stripPointerCasts() to ptr above
1691 cgm.errorNYI(e->getSourceRange(), "emitMaterializeTemporaryExpr: GlobalOp");
1692 return {};
1693 } else {
1694 assert(!cir::MissingFeatures::emitLifetimeMarkers());
1695 emitAnyExprToMem(e, object, Qualifiers(), /*isInitializer=*/true);
1696 }
1697 pushTemporaryCleanup(*this, m, e, object);
1698
1699 // Perform derived-to-base casts and/or field accesses, to get from the
1700 // temporary object we created (and, potentially, for which we extended
1701 // the lifetime) to the subobject we're binding the reference to.
1702 if (!adjustments.empty()) {
1703 cgm.errorNYI(e->getSourceRange(),
1704 "emitMaterializeTemporaryExpr: Adjustments");
1705 return {};
1706 }
1707
1708 return makeAddrLValue(object, m->getType(), AlignmentSource::Decl);
1709 }
1710
1711 LValue
1712 CIRGenFunction::getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e) {
1713 assert(OpaqueValueMapping::shouldBindAsLValue(e));
1714
1715 auto it = opaqueLValues.find(e);
1716 if (it != opaqueLValues.end())
1717 return it->second;
1718
1719 assert(e->isUnique() && "LValue for a nonunique OVE hasn't been emitted");
1720 return emitLValue(e->getSourceExpr());
1721 }
1722
1723 RValue
1724 CIRGenFunction::getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e) {
1725 assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1726
1727 auto it = opaqueRValues.find(e);
1728 if (it != opaqueRValues.end())
1729 return it->second;
1730
1731 assert(e->isUnique() && "RValue for a nonunique OVE hasn't been emitted");
1732 return emitAnyExpr(e->getSourceExpr());
1733 }
1734
1735 LValue CIRGenFunction::emitCompoundLiteralLValue(const CompoundLiteralExpr *e) {
1736 if (e->isFileScope()) {
1737 cgm.errorNYI(e->getSourceRange(), "emitCompoundLiteralLValue: FileScope");
1738 return {};
1739 }
1740
1741 if (e->getType()->isVariablyModifiedType()) {
1742 cgm.errorNYI(e->getSourceRange(),
1743 "emitCompoundLiteralLValue: VariablyModifiedType");
1744 return {};
1745 }
1746
1747 Address declPtr = createMemTemp(e->getType(), getLoc(e->getSourceRange()),
1748 ".compoundliteral");
1749 const Expr *initExpr = e->getInitializer();
1750 LValue result = makeAddrLValue(declPtr, e->getType(), AlignmentSource::Decl);
1751
1752 emitAnyExprToMem(initExpr, declPtr, e->getType().getQualifiers(),
1753 /*Init*/ true);
1754
1755 // Block-scope compound literals are destroyed at the end of the enclosing
1756 // scope in C.
1757 if (!getLangOpts().CPlusPlus && e->getType().isDestructedType()) {
1758 cgm.errorNYI(e->getSourceRange(),
1759 "emitCompoundLiteralLValue: non C++ DestructedType");
1760 return {};
1761 }
1762
1763 return result;
1764 }
1765
1766 LValue CIRGenFunction::emitCallExprLValue(const CallExpr *e) {
1767 RValue rv = emitCallExpr(e);
1768
1769 if (!rv.isScalar()) {
1770 cgm.errorNYI(e->getSourceRange(), "emitCallExprLValue: non-scalar return");
1771 return {};
1772 }
1773
1774 assert(e->getCallReturnType(getContext())->isReferenceType() &&
1775 "Can't have a scalar return unless the return type is a "
1776 "reference type!");
1777
1778 return makeNaturalAlignPointeeAddrLValue(rv.getValue(), e->getType());
1779 }
1780
1781 LValue CIRGenFunction::emitBinaryOperatorLValue(const BinaryOperator *e) {
1782 // Comma expressions just emit their LHS then their RHS as an l-value.
1783 if (e->getOpcode() == BO_Comma) {
1784 emitIgnoredExpr(e->getLHS());
1785 return emitLValue(e->getRHS());
1786 }
1787
1788 if (e->getOpcode() == BO_PtrMemD || e->getOpcode() == BO_PtrMemI)
1789 return emitPointerToDataMemberBinaryExpr(e);
1790
1791 assert(e->getOpcode() == BO_Assign && "unexpected binary l-value");
1792
1793 // Note that in all of these cases, __block variables need the RHS
1794 // evaluated first just in case the variable gets moved by the RHS.
1795
1796 switch (CIRGenFunction::getEvaluationKind(e->getType())) {
1797 case cir::TEK_Scalar: {
1798 assert(!cir::MissingFeatures::objCLifetime());
1799 if (e->getLHS()->getType().getObjCLifetime() !=
1800 clang::Qualifiers::ObjCLifetime::OCL_None) {
1801 cgm.errorNYI(e->getSourceRange(), "objc lifetimes");
1802 return {};
1803 }
1804
1805 RValue rv = emitAnyExpr(e->getRHS());
1806 LValue lv = emitLValue(e->getLHS());
1807
1808 SourceLocRAIIObject loc{*this, getLoc(e->getSourceRange())};
1809 if (lv.isBitField())
1810 emitStoreThroughBitfieldLValue(rv, lv);
1811 else
1812 emitStoreThroughLValue(rv, lv);
1813
1814 if (getLangOpts().OpenMP) {
1815 cgm.errorNYI(e->getSourceRange(), "openmp");
1816 return {};
1817 }
1818
1819 return lv;
1820 }
1821
1822 case cir::TEK_Complex: {
1823 return emitComplexAssignmentLValue(e);
1824 }
1825
1826 case cir::TEK_Aggregate:
1827 cgm.errorNYI(e->getSourceRange(), "aggregate lvalues");
1828 return {};
1829 }
1830 llvm_unreachable("bad evaluation kind");
1831 }
1832
1833 /// Emit code to compute the specified expression which
1834 /// can have any type. The result is returned as an RValue struct.
1835 RValue CIRGenFunction::emitAnyExpr(const Expr *e, AggValueSlot aggSlot,
1836 bool ignoreResult) {
1837 switch (CIRGenFunction::getEvaluationKind(e->getType())) {
1838 case cir::TEK_Scalar:
1839 return RValue::get(emitScalarExpr(e, ignoreResult));
1840 case cir::TEK_Complex:
1841 return RValue::getComplex(emitComplexExpr(e));
1842 case cir::TEK_Aggregate: {
1843 if (!ignoreResult && aggSlot.isIgnored())
1844 aggSlot = createAggTemp(e->getType(), getLoc(e->getSourceRange()),
1845 getCounterAggTmpAsString());
1846 emitAggExpr(e, aggSlot);
1847 return aggSlot.asRValue();
1848 }
1849 }
1850 llvm_unreachable("bad evaluation kind");
1851 }
1852
1853 // Detect the unusual situation where an inline version is shadowed by a
1854 // non-inline version. In that case we should pick the external one
1855 // everywhere. That's GCC behavior too.
1856 static bool onlyHasInlineBuiltinDeclaration(const FunctionDecl *fd) {
1857 for (const FunctionDecl *pd = fd; pd; pd = pd->getPreviousDecl())
1858 if (!pd->isInlineBuiltinDeclaration())
1859 return false;
1860 return true;
1861 }
1862
1863 CIRGenCallee CIRGenFunction::emitDirectCallee(const GlobalDecl &gd) {
1864 const auto *fd = cast<FunctionDecl>(gd.getDecl());
1865
1866 if (unsigned builtinID = fd->getBuiltinID()) {
1867 StringRef ident = cgm.getMangledName(gd);
1868 std::string fdInlineName = (ident + ".inline").str();
1869
1870 bool isPredefinedLibFunction =
1871 cgm.getASTContext().BuiltinInfo.isPredefinedLibFunction(builtinID);
1872 // Assume nobuiltins everywhere until we actually read the attributes.
1873 bool hasAttributeNoBuiltin = true;
1874 assert(!cir::MissingFeatures::attributeNoBuiltin());
1875
1876 // When directing calling an inline builtin, call it through it's mangled
1877 // name to make it clear it's not the actual builtin.
1878 auto fn = cast<cir::FuncOp>(curFn);
1879 if (fn.getName() != fdInlineName && onlyHasInlineBuiltinDeclaration(fd)) {
1880 cir::FuncOp clone =
1881 mlir::cast_or_null<cir::FuncOp>(cgm.getGlobalValue(fdInlineName));
1882
1883 if (!clone) {
1884 // Create a forward declaration - the body will be generated in
1885 // generateCode when the function definition is processed
1886 cir::FuncOp calleeFunc = emitFunctionDeclPointer(cgm, gd);
1887 mlir::OpBuilder::InsertionGuard guard(builder);
1888 builder.setInsertionPointToStart(cgm.getModule().getBody());
1889
1890 clone = cir::FuncOp::create(builder, calleeFunc.getLoc(), fdInlineName,
1891 calleeFunc.getFunctionType());
1892 clone.setLinkageAttr(cir::GlobalLinkageKindAttr::get(
1893 &cgm.getMLIRContext(), cir::GlobalLinkageKind::InternalLinkage));
1894 clone.setSymVisibility("private");
1895 clone.setInlineKind(cir::InlineKind::AlwaysInline);
1896 }
1897 return CIRGenCallee::forDirect(clone, gd);
1898 }
1899
1900 // Replaceable builtins provide their own implementation of a builtin. If we
1901 // are in an inline builtin implementation, avoid trivial infinite
1902 // recursion. Honor __attribute__((no_builtin("foo"))) or
1903 // __attribute__((no_builtin)) on the current function unless foo is
1904 // not a predefined library function which means we must generate the
1905 // builtin no matter what.
1906 else if (!isPredefinedLibFunction || !hasAttributeNoBuiltin)
1907 return CIRGenCallee::forBuiltin(builtinID, fd);
1908 }
1909
1910 cir::FuncOp callee = emitFunctionDeclPointer(cgm, gd);
1911
1912 assert(!cir::MissingFeatures::hip());
1913
1914 return CIRGenCallee::forDirect(callee, gd);
1915 }
1916
1917 RValue CIRGenFunction::getUndefRValue(QualType ty) {
1918 if (ty->isVoidType())
1919 return RValue::get(nullptr);
1920
1921 cgm.errorNYI("unsupported type for undef rvalue");
1922 return RValue::get(nullptr);
1923 }
1924
1925 RValue CIRGenFunction::emitCall(clang::QualType calleeTy,
1926 const CIRGenCallee &origCallee,
1927 const clang::CallExpr *e,
1928 ReturnValueSlot returnValue) {
1929 // Get the actual function type. The callee type will always be a pointer to
1930 // function type or a block pointer type.
1931 assert(calleeTy->isFunctionPointerType() &&
1932 "Callee must have function pointer type!");
1933
1934 calleeTy = getContext().getCanonicalType(calleeTy);
1935 auto pointeeTy = cast<PointerType>(calleeTy)->getPointeeType();
1936
1937 CIRGenCallee callee = origCallee;
1938
1939 if (getLangOpts().CPlusPlus)
1940 assert(!cir::MissingFeatures::sanitizers());
1941
1942 const auto *fnType = cast<FunctionType>(pointeeTy);
1943
1944 assert(!cir::MissingFeatures::sanitizers());
1945
1946 CallArgList args;
1947 assert(!cir::MissingFeatures::opCallArgEvaluationOrder());
1948
1949 emitCallArgs(args, dyn_cast<FunctionProtoType>(fnType), e->arguments(),
1950 e->getDirectCallee());
1951
1952 const CIRGenFunctionInfo &funcInfo =
1953 cgm.getTypes().arrangeFreeFunctionCall(args, fnType);
1954
1955 // C99 6.5.2.2p6:
1956 // If the expression that denotes the called function has a type that does
1957 // not include a prototype, [the default argument promotions are performed].
1958 // If the number of arguments does not equal the number of parameters, the
1959 // behavior is undefined. If the function is defined with a type that
1960 // includes a prototype, and either the prototype ends with an ellipsis (,
1961 // ...) or the types of the arguments after promotion are not compatible
1962 // with the types of the parameters, the behavior is undefined. If the
1963 // function is defined with a type that does not include a prototype, and
1964 // the types of the arguments after promotion are not compatible with those
1965 // of the parameters after promotion, the behavior is undefined [except in
1966 // some trivial cases].
1967 // That is, in the general case, we should assume that a call through an
1968 // unprototyped function type works like a *non-variadic* call. The way we
1969 // make this work is to cast to the exxact type fo the promoted arguments.
1970 if (isa<FunctionNoProtoType>(fnType)) {
1971 assert(!cir::MissingFeatures::opCallChain());
1972 assert(!cir::MissingFeatures::addressSpace());
1973 cir::FuncType calleeTy = getTypes().getFunctionType(funcInfo);
1974 // get non-variadic function type
1975 calleeTy = cir::FuncType::get(calleeTy.getInputs(),
1976 calleeTy.getReturnType(), false);
1977 auto calleePtrTy = cir::PointerType::get(calleeTy);
1978
1979 mlir::Operation *fn = callee.getFunctionPointer();
1980 mlir::Value addr;
1981 if (auto funcOp = mlir::dyn_cast<cir::FuncOp>(fn)) {
1982 addr = cir::GetGlobalOp::create(
1983 builder, getLoc(e->getSourceRange()),
1984 cir::PointerType::get(funcOp.getFunctionType()), funcOp.getSymName());
1985 } else {
1986 addr = fn->getResult(0);
1987 }
1988
1989 fn = builder.createBitcast(addr, calleePtrTy).getDefiningOp();
1990 callee.setFunctionPointer(fn);
1991 }
1992
1993 assert(!cir::MissingFeatures::opCallFnInfoOpts());
1994 assert(!cir::MissingFeatures::hip());
1995 assert(!cir::MissingFeatures::opCallMustTail());
1996
1997 cir::CIRCallOpInterface callOp;
1998 RValue callResult = emitCall(funcInfo, callee, returnValue, args, &callOp,
1999 getLoc(e->getExprLoc()));
2000
2001 assert(!cir::MissingFeatures::generateDebugInfo());
2002
2003 return callResult;
2004 }
2005
2006 CIRGenCallee CIRGenFunction::emitCallee(const clang::Expr *e) {
2007 e = e->IgnoreParens();
2008
2009 // Look through function-to-pointer decay.
2010 if (const auto *implicitCast = dyn_cast<ImplicitCastExpr>(e)) {
2011 if (implicitCast->getCastKind() == CK_FunctionToPointerDecay ||
2012 implicitCast->getCastKind() == CK_BuiltinFnToFnPtr) {
2013 return emitCallee(implicitCast->getSubExpr());
2014 }
2015 // When performing an indirect call through a function pointer lvalue, the
2016 // function pointer lvalue is implicitly converted to an rvalue through an
2017 // lvalue-to-rvalue conversion.
2018 assert(implicitCast->getCastKind() == CK_LValueToRValue &&
2019 "unexpected implicit cast on function pointers");
2020 } else if (const auto *declRef = dyn_cast<DeclRefExpr>(e)) {
2021 // Resolve direct calls.
2022 const auto *funcDecl = cast<FunctionDecl>(declRef->getDecl());
2023 return emitDirectCallee(funcDecl);
2024 } else if (auto me = dyn_cast<MemberExpr>(e)) {
2025 if (const auto *fd = dyn_cast<FunctionDecl>(me->getMemberDecl())) {
2026 emitIgnoredExpr(me->getBase());
2027 return emitDirectCallee(fd);
2028 }
2029 // Else fall through to the indirect reference handling below.
2030 } else if (auto *pde = dyn_cast<CXXPseudoDestructorExpr>(e)) {
2031 return CIRGenCallee::forPseudoDestructor(pde);
2032 }
2033
2034 // Otherwise, we have an indirect reference.
2035 mlir::Value calleePtr;
2036 QualType functionType;
2037 if (const auto *ptrType = e->getType()->getAs<clang::PointerType>()) {
2038 calleePtr = emitScalarExpr(e);
2039 functionType = ptrType->getPointeeType();
2040 } else {
2041 functionType = e->getType();
2042 calleePtr = emitLValue(e).getPointer();
2043 }
2044 assert(functionType->isFunctionType());
2045
2046 GlobalDecl gd;
2047 if (const auto *vd =
2048 dyn_cast_or_null<VarDecl>(e->getReferencedDeclOfCallee()))
2049 gd = GlobalDecl(vd);
2050
2051 CIRGenCalleeInfo calleeInfo(functionType->getAs<FunctionProtoType>(), gd);
2052 CIRGenCallee callee(calleeInfo, calleePtr.getDefiningOp());
2053 return callee;
2054 }
2055
2056 RValue CIRGenFunction::emitCallExpr(const clang::CallExpr *e,
2057 ReturnValueSlot returnValue) {
2058 assert(!cir::MissingFeatures::objCBlocks());
2059
2060 if (const auto *ce = dyn_cast<CXXMemberCallExpr>(e))
2061 return emitCXXMemberCallExpr(ce, returnValue);
2062
2063 if (isa<CUDAKernelCallExpr>(e)) {
2064 cgm.errorNYI(e->getSourceRange(), "call to CUDA kernel");
2065 return RValue::get(nullptr);
2066 }
2067
2068 if (const auto *operatorCall = dyn_cast<CXXOperatorCallExpr>(e)) {
2069 // If the callee decl is a CXXMethodDecl, we need to emit this as a C++
2070 // operator member call.
2071 if (const CXXMethodDecl *md =
2072 dyn_cast_or_null<CXXMethodDecl>(operatorCall->getCalleeDecl()))
2073 return emitCXXOperatorMemberCallExpr(operatorCall, md, returnValue);
2074 // A CXXOperatorCallExpr is created even for explicit object methods, but
2075 // these should be treated like static function calls. Fall through to do
2076 // that.
2077 }
2078
2079 CIRGenCallee callee = emitCallee(e->getCallee());
2080
2081 if (callee.isBuiltin())
2082 return emitBuiltinExpr(callee.getBuiltinDecl(), callee.getBuiltinID(), e,
2083 returnValue);
2084
2085 if (callee.isPseudoDestructor())
2086 return emitCXXPseudoDestructorExpr(callee.getPseudoDestructorExpr());
2087
2088 return emitCall(e->getCallee()->getType(), callee, e, returnValue);
2089 }
2090
2091 /// Emit code to compute the specified expression, ignoring the result.
2092 void CIRGenFunction::emitIgnoredExpr(const Expr *e) {
2093 if (e->isPRValue()) {
2094 emitAnyExpr(e, AggValueSlot::ignored(), /*ignoreResult=*/true);
2095 return;
2096 }
2097
2098 // Just emit it as an l-value and drop the result.
2099 emitLValue(e);
2100 }
2101
2102 Address CIRGenFunction::emitArrayToPointerDecay(const Expr *e,
2103 LValueBaseInfo *baseInfo) {
2104 assert(!cir::MissingFeatures::opTBAA());
2105 assert(e->getType()->isArrayType() &&
2106 "Array to pointer decay must have array source type!");
2107
2108 // Expressions of array type can't be bitfields or vector elements.
2109 LValue lv = emitLValue(e);
2110 Address addr = lv.getAddress();
2111
2112 // If the array type was an incomplete type, we need to make sure
2113 // the decay ends up being the right type.
2114 auto lvalueAddrTy = mlir::cast<cir::PointerType>(addr.getPointer().getType());
2115
2116 if (e->getType()->isVariableArrayType())
2117 return addr;
2118
2119 [[maybe_unused]] auto pointeeTy =
2120 mlir::cast<cir::ArrayType>(lvalueAddrTy.getPointee());
2121
2122 [[maybe_unused]] mlir::Type arrayTy = convertType(e->getType());
2123 assert(mlir::isa<cir::ArrayType>(arrayTy) && "expected array");
2124 assert(pointeeTy == arrayTy);
2125
2126 // The result of this decay conversion points to an array element within the
2127 // base lvalue. However, since TBAA currently does not support representing
2128 // accesses to elements of member arrays, we conservatively represent accesses
2129 // to the pointee object as if it had no any base lvalue specified.
2130 // TODO: Support TBAA for member arrays.
2131 QualType eltType = e->getType()->castAsArrayTypeUnsafe()->getElementType();
2132 assert(!cir::MissingFeatures::opTBAA());
2133
2134 mlir::Value ptr = builder.maybeBuildArrayDecay(
2135 cgm.getLoc(e->getSourceRange()), addr.getPointer(),
2136 convertTypeForMem(eltType));
2137 return Address(ptr, addr.getAlignment());
2138 }
2139
2140 /// Given the address of a temporary variable, produce an r-value of its type.
2141 RValue CIRGenFunction::convertTempToRValue(Address addr, clang::QualType type,
2142 clang::SourceLocation loc) {
2143 LValue lvalue = makeAddrLValue(addr, type, AlignmentSource::Decl);
2144 switch (getEvaluationKind(type)) {
2145 case cir::TEK_Complex:
2146 return RValue::getComplex(emitLoadOfComplex(lvalue, loc));
2147 case cir::TEK_Aggregate:
2148 return lvalue.asAggregateRValue();
2149 case cir::TEK_Scalar:
2150 return RValue::get(emitLoadOfScalar(lvalue, loc));
2151 }
2152 llvm_unreachable("bad evaluation kind");
2153 }
2154
2155 /// Emit an `if` on a boolean condition, filling `then` and `else` into
2156 /// appropriated regions.
2157 mlir::LogicalResult CIRGenFunction::emitIfOnBoolExpr(const Expr *cond,
2158 const Stmt *thenS,
2159 const Stmt *elseS) {
2160 mlir::Location thenLoc = getLoc(thenS->getSourceRange());
2161 std::optional<mlir::Location> elseLoc;
2162 if (elseS)
2163 elseLoc = getLoc(elseS->getSourceRange());
2164
2165 mlir::LogicalResult resThen = mlir::success(), resElse = mlir::success();
2166 emitIfOnBoolExpr(
2167 cond, /*thenBuilder=*/
2168 [&](mlir::OpBuilder &, mlir::Location) {
2169 LexicalScope lexScope{*this, thenLoc, builder.getInsertionBlock()};
2170 resThen = emitStmt(thenS, /*useCurrentScope=*/true);
2171 },
2172 thenLoc,
2173 /*elseBuilder=*/
2174 [&](mlir::OpBuilder &, mlir::Location) {
2175 assert(elseLoc && "Invalid location for elseS.");
2176 LexicalScope lexScope{*this, *elseLoc, builder.getInsertionBlock()};
2177 resElse = emitStmt(elseS, /*useCurrentScope=*/true);
2178 },
2179 elseLoc);
2180
2181 return mlir::LogicalResult::success(resThen.succeeded() &&
2182 resElse.succeeded());
2183 }
2184
2185 /// Emit an `if` on a boolean condition, filling `then` and `else` into
2186 /// appropriated regions.
2187 cir::IfOp CIRGenFunction::emitIfOnBoolExpr(
2188 const clang::Expr *cond, BuilderCallbackRef thenBuilder,
2189 mlir::Location thenLoc, BuilderCallbackRef elseBuilder,
2190 std::optional<mlir::Location> elseLoc) {
2191 // Attempt to be as accurate as possible with IfOp location, generate
2192 // one fused location that has either 2 or 4 total locations, depending
2193 // on else's availability.
2194 SmallVector<mlir::Location, 2> ifLocs{thenLoc};
2195 if (elseLoc)
2196 ifLocs.push_back(*elseLoc);
2197 mlir::Location loc = mlir::FusedLoc::get(&getMLIRContext(), ifLocs);
2198
2199 // Emit the code with the fully general case.
2200 mlir::Value condV = emitOpOnBoolExpr(loc, cond);
2201 return cir::IfOp::create(builder, loc, condV, elseLoc.has_value(),
2202 /*thenBuilder=*/thenBuilder,
2203 /*elseBuilder=*/elseBuilder);
2204 }
2205
2206 /// TODO(cir): see EmitBranchOnBoolExpr for extra ideas).
2207 mlir::Value CIRGenFunction::emitOpOnBoolExpr(mlir::Location loc,
2208 const Expr *cond) {
2209 assert(!cir::MissingFeatures::pgoUse());
2210 assert(!cir::MissingFeatures::generateDebugInfo());
2211 cond = cond->IgnoreParens();
2212
2213 // In LLVM the condition is reversed here for efficient codegen.
2214 // This should be done in CIR prior to LLVM lowering, if we do now
2215 // we can make CIR based diagnostics misleading.
2216 // cir.ternary(!x, t, f) -> cir.ternary(x, f, t)
2217 assert(!cir::MissingFeatures::shouldReverseUnaryCondOnBoolExpr());
2218
2219 if (const ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(cond)) {
2220 Expr *trueExpr = condOp->getTrueExpr();
2221 Expr *falseExpr = condOp->getFalseExpr();
2222 mlir::Value condV = emitOpOnBoolExpr(loc, condOp->getCond());
2223
2224 mlir::Value ternaryOpRes =
2225 cir::TernaryOp::create(
2226 builder, loc, condV, /*thenBuilder=*/
2227 [this, trueExpr](mlir::OpBuilder &b, mlir::Location loc) {
2228 mlir::Value lhs = emitScalarExpr(trueExpr);
2229 cir::YieldOp::create(b, loc, lhs);
2230 },
2231 /*elseBuilder=*/
2232 [this, falseExpr](mlir::OpBuilder &b, mlir::Location loc) {
2233 mlir::Value rhs = emitScalarExpr(falseExpr);
2234 cir::YieldOp::create(b, loc, rhs);
2235 })
2236 .getResult();
2237
2238 return emitScalarConversion(ternaryOpRes, condOp->getType(),
2239 getContext().BoolTy, condOp->getExprLoc());
2240 }
2241
2242 if (isa<CXXThrowExpr>(cond)) {
2243 cgm.errorNYI("NYI");
2244 return createDummyValue(loc, cond->getType());
2245 }
2246
2247 // If the branch has a condition wrapped by __builtin_unpredictable,
2248 // create metadata that specifies that the branch is unpredictable.
2249 // Don't bother if not optimizing because that metadata would not be used.
2250 assert(!cir::MissingFeatures::insertBuiltinUnpredictable());
2251
2252 // Emit the code with the fully general case.
2253 return evaluateExprAsBool(cond);
2254 }
2255
2256 mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
2257 mlir::Location loc, CharUnits alignment,
2258 bool insertIntoFnEntryBlock,
2259 mlir::Value arraySize) {
2260 mlir::Block *entryBlock = insertIntoFnEntryBlock
2261 ? getCurFunctionEntryBlock()
2262 : curLexScope->getEntryBlock();
2263
2264 // If this is an alloca in the entry basic block of a cir.try and there's
2265 // a surrounding cir.scope, make sure the alloca ends up in the surrounding
2266 // scope instead. This is necessary in order to guarantee all SSA values are
2267 // reachable during cleanups.
2268 if (auto tryOp =
2269 llvm::dyn_cast_if_present<cir::TryOp>(entryBlock->getParentOp())) {
2270 if (auto scopeOp = llvm::dyn_cast<cir::ScopeOp>(tryOp->getParentOp()))
2271 entryBlock = &scopeOp.getScopeRegion().front();
2272 }
2273
2274 return emitAlloca(name, ty, loc, alignment,
2275 builder.getBestAllocaInsertPoint(entryBlock), arraySize);
2276 }
2277
2278 mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
2279 mlir::Location loc, CharUnits alignment,
2280 mlir::OpBuilder::InsertPoint ip,
2281 mlir::Value arraySize) {
2282 // CIR uses its own alloca address space rather than follow the target data
2283 // layout like original CodeGen. The data layout awareness should be done in
2284 // the lowering pass instead.
2285 cir::PointerType localVarPtrTy =
2286 builder.getPointerTo(ty, getCIRAllocaAddressSpace());
2287 mlir::IntegerAttr alignIntAttr = cgm.getSize(alignment);
2288
2289 mlir::Value addr;
2290 {
2291 mlir::OpBuilder::InsertionGuard guard(builder);
2292 builder.restoreInsertionPoint(ip);
2293 addr = builder.createAlloca(loc, /*addr type*/ localVarPtrTy,
2294 /*var type*/ ty, name, alignIntAttr, arraySize);
2295 assert(!cir::MissingFeatures::astVarDeclInterface());
2296 }
2297 return addr;
2298 }
2299
2300 // Note: this function also emit constructor calls to support a MSVC extensions
2301 // allowing explicit constructor function call.
2302 RValue CIRGenFunction::emitCXXMemberCallExpr(const CXXMemberCallExpr *ce,
2303 ReturnValueSlot returnValue) {
2304 const Expr *callee = ce->getCallee()->IgnoreParens();
2305
2306 if (isa<BinaryOperator>(callee)) {
2307 cgm.errorNYI(ce->getSourceRange(),
2308 "emitCXXMemberCallExpr: C++ binary operator");
2309 return RValue::get(nullptr);
2310 }
2311
2312 const auto *me = cast<MemberExpr>(callee);
2313 const auto *md = cast<CXXMethodDecl>(me->getMemberDecl());
2314
2315 if (md->isStatic()) {
2316 cgm.errorNYI(ce->getSourceRange(), "emitCXXMemberCallExpr: static method");
2317 return RValue::get(nullptr);
2318 }
2319
2320 bool hasQualifier = me->hasQualifier();
2321 NestedNameSpecifier qualifier = me->getQualifier();
2322 bool isArrow = me->isArrow();
2323 const Expr *base = me->getBase();
2324
2325 return emitCXXMemberOrOperatorMemberCallExpr(
2326 ce, md, returnValue, hasQualifier, qualifier, isArrow, base);
2327 }
2328
2329 RValue CIRGenFunction::emitReferenceBindingToExpr(const Expr *e) {
2330 // Emit the expression as an lvalue.
2331 LValue lv = emitLValue(e);
2332 assert(lv.isSimple());
2333 mlir::Value value = lv.getPointer();
2334
2335 assert(!cir::MissingFeatures::sanitizers());
2336
2337 return RValue::get(value);
2338 }
2339
2340 Address CIRGenFunction::emitLoadOfReference(LValue refLVal, mlir::Location loc,
2341 LValueBaseInfo *pointeeBaseInfo) {
2342 if (refLVal.isVolatile())
2343 cgm.errorNYI(loc, "load of volatile reference");
2344
2345 cir::LoadOp load =
2346 cir::LoadOp::create(builder, loc, refLVal.getAddress().getElementType(),
2347 refLVal.getAddress().getPointer());
2348
2349 assert(!cir::MissingFeatures::opTBAA());
2350
2351 QualType pointeeType = refLVal.getType()->getPointeeType();
2352 CharUnits align = cgm.getNaturalTypeAlignment(pointeeType, pointeeBaseInfo);
2353 return Address(load, convertTypeForMem(pointeeType), align);
2354 }
2355
2356 LValue CIRGenFunction::emitLoadOfReferenceLValue(Address refAddr,
2357 mlir::Location loc,
2358 QualType refTy,
2359 AlignmentSource source) {
2360 LValue refLVal = makeAddrLValue(refAddr, refTy, LValueBaseInfo(source));
2361 LValueBaseInfo pointeeBaseInfo;
2362 assert(!cir::MissingFeatures::opTBAA());
2363 Address pointeeAddr = emitLoadOfReference(refLVal, loc, &pointeeBaseInfo);
2364 return makeAddrLValue(pointeeAddr, refLVal.getType()->getPointeeType(),
2365 pointeeBaseInfo);
2366 }
2367
2368 void CIRGenFunction::emitTrap(mlir::Location loc, bool createNewBlock) {
2369 cir::TrapOp::create(builder, loc);
2370 if (createNewBlock)
2371 builder.createBlock(builder.getBlock()->getParent());
2372 }
2373
2374 void CIRGenFunction::emitUnreachable(clang::SourceLocation loc,
2375 bool createNewBlock) {
2376 assert(!cir::MissingFeatures::sanitizers());
2377 cir::UnreachableOp::create(builder, getLoc(loc));
2378 if (createNewBlock)
2379 builder.createBlock(builder.getBlock()->getParent());
2380 }
2381
2382 mlir::Value CIRGenFunction::createDummyValue(mlir::Location loc,
2383 clang::QualType qt) {
2384 mlir::Type t = convertType(qt);
2385 CharUnits alignment = getContext().getTypeAlignInChars(qt);
2386 return builder.createDummyValue(loc, t, alignment);
2387 }
2388
2389 //===----------------------------------------------------------------------===//
2390 // CIR builder helpers
2391 //===----------------------------------------------------------------------===//
2392
2393 Address CIRGenFunction::createMemTemp(QualType ty, mlir::Location loc,
2394 const Twine &name, Address *alloca,
2395 mlir::OpBuilder::InsertPoint ip) {
2396 // FIXME: Should we prefer the preferred type alignment here?
2397 return createMemTemp(ty, getContext().getTypeAlignInChars(ty), loc, name,
2398 alloca, ip);
2399 }
2400
2401 Address CIRGenFunction::createMemTemp(QualType ty, CharUnits align,
2402 mlir::Location loc, const Twine &name,
2403 Address *alloca,
2404 mlir::OpBuilder::InsertPoint ip) {
2405 Address result = createTempAlloca(convertTypeForMem(ty), align, loc, name,
2406 /*ArraySize=*/nullptr, alloca, ip);
2407 if (ty->isConstantMatrixType()) {
2408 assert(!cir::MissingFeatures::matrixType());
2409 cgm.errorNYI(loc, "temporary matrix value");
2410 }
2411 return result;
2412 }
2413
2414 /// This creates a alloca and inserts it into the entry block of the
2415 /// current region.
2416 Address CIRGenFunction::createTempAllocaWithoutCast(
2417 mlir::Type ty, CharUnits align, mlir::Location loc, const Twine &name,
2418 mlir::Value arraySize, mlir::OpBuilder::InsertPoint ip) {
2419 cir::AllocaOp alloca = ip.isSet()
2420 ? createTempAlloca(ty, loc, name, ip, arraySize)
2421 : createTempAlloca(ty, loc, name, arraySize);
2422 alloca.setAlignmentAttr(cgm.getSize(align));
2423 return Address(alloca, ty, align);
2424 }
2425
2426 /// This creates a alloca and inserts it into the entry block. The alloca is
2427 /// casted to default address space if necessary.
2428 // TODO(cir): Implement address space casting to match classic codegen's
2429 // CreateTempAlloca behavior with DestLangAS parameter
2430 Address CIRGenFunction::createTempAlloca(mlir::Type ty, CharUnits align,
2431 mlir::Location loc, const Twine &name,
2432 mlir::Value arraySize,
2433 Address *allocaAddr,
2434 mlir::OpBuilder::InsertPoint ip) {
2435 Address alloca =
2436 createTempAllocaWithoutCast(ty, align, loc, name, arraySize, ip);
2437 if (allocaAddr)
2438 *allocaAddr = alloca;
2439 mlir::Value v = alloca.getPointer();
2440 // Alloca always returns a pointer in alloca address space, which may
2441 // be different from the type defined by the language. For example,
2442 // in C++ the auto variables are in the default address space. Therefore
2443 // cast alloca to the default address space when necessary.
2444
2445 LangAS allocaAS = alloca.getAddressSpace()
2446 ? clang::getLangASFromTargetAS(
2447 alloca.getAddressSpace().getValue().getUInt())
2448 : clang::LangAS::Default;
2449 LangAS dstTyAS = clang::LangAS::Default;
2450 if (getCIRAllocaAddressSpace()) {
2451 dstTyAS = clang::getLangASFromTargetAS(
2452 getCIRAllocaAddressSpace().getValue().getUInt());
2453 }
2454
2455 if (dstTyAS != allocaAS) {
2456 getTargetHooks().performAddrSpaceCast(*this, v, getCIRAllocaAddressSpace(),
2457 builder.getPointerTo(ty, dstTyAS));
2458 }
2459 return Address(v, ty, align);
2460 }
2461
2462 /// This creates an alloca and inserts it into the entry block if \p ArraySize
2463 /// is nullptr, otherwise inserts it at the current insertion point of the
2464 /// builder.
2465 cir::AllocaOp CIRGenFunction::createTempAlloca(mlir::Type ty,
2466 mlir::Location loc,
2467 const Twine &name,
2468 mlir::Value arraySize,
2469 bool insertIntoFnEntryBlock) {
2470 return mlir::cast<cir::AllocaOp>(emitAlloca(name.str(), ty, loc, CharUnits(),
2471 insertIntoFnEntryBlock, arraySize)
2472 .getDefiningOp());
2473 }
2474
2475 /// This creates an alloca and inserts it into the provided insertion point
2476 cir::AllocaOp CIRGenFunction::createTempAlloca(mlir::Type ty,
2477 mlir::Location loc,
2478 const Twine &name,
2479 mlir::OpBuilder::InsertPoint ip,
2480 mlir::Value arraySize) {
2481 assert(ip.isSet() && "Insertion point is not set");
2482 return mlir::cast<cir::AllocaOp>(
2483 emitAlloca(name.str(), ty, loc, CharUnits(), ip, arraySize)
2484 .getDefiningOp());
2485 }
2486
2487 /// Try to emit a reference to the given value without producing it as
2488 /// an l-value. For many cases, this is just an optimization, but it avoids
2489 /// us needing to emit global copies of variables if they're named without
2490 /// triggering a formal use in a context where we can't emit a direct
2491 /// reference to them, for instance if a block or lambda or a member of a
2492 /// local class uses a const int variable or constexpr variable from an
2493 /// enclosing function.
2494 ///
2495 /// For named members of enums, this is the only way they are emitted.
2496 CIRGenFunction::ConstantEmission
2497 CIRGenFunction::tryEmitAsConstant(const DeclRefExpr *refExpr) {
2498 const ValueDecl *value = refExpr->getDecl();
2499
2500 // There is a lot more to do here, but for now only EnumConstantDecl is
2501 // supported.
2502 assert(!cir::MissingFeatures::tryEmitAsConstant());
2503
2504 // The value needs to be an enum constant or a constant variable.
2505 if (!isa<EnumConstantDecl>(value))
2506 return ConstantEmission();
2507
2508 Expr::EvalResult result;
2509 if (!refExpr->EvaluateAsRValue(result, getContext()))
2510 return ConstantEmission();
2511
2512 QualType resultType = refExpr->getType();
2513
2514 // As long as we're only handling EnumConstantDecl, there should be no
2515 // side-effects.
2516 assert(!result.HasSideEffects);
2517
2518 // Emit as a constant.
2519 // FIXME(cir): have emitAbstract build a TypedAttr instead (this requires
2520 // somewhat heavy refactoring...)
2521 mlir::Attribute c = ConstantEmitter(*this).emitAbstract(
2522 refExpr->getLocation(), result.Val, resultType);
2523 mlir::TypedAttr cstToEmit = mlir::dyn_cast_if_present<mlir::TypedAttr>(c);
2524 assert(cstToEmit && "expected a typed attribute");
2525
2526 assert(!cir::MissingFeatures::generateDebugInfo());
2527
2528 return ConstantEmission::forValue(cstToEmit);
2529 }
2530
2531 CIRGenFunction::ConstantEmission
2532 CIRGenFunction::tryEmitAsConstant(const MemberExpr *me) {
2533 if (DeclRefExpr *dre = tryToConvertMemberExprToDeclRefExpr(*this, me))
2534 return tryEmitAsConstant(dre);
2535 return ConstantEmission();
2536 }
2537
2538 mlir::Value CIRGenFunction::emitScalarConstant(
2539 const CIRGenFunction::ConstantEmission &constant, Expr *e) {
2540 assert(constant && "not a constant");
2541 if (constant.isReference()) {
2542 cgm.errorNYI(e->getSourceRange(), "emitScalarConstant: reference");
2543 return {};
2544 }
2545 return builder.getConstant(getLoc(e->getSourceRange()), constant.getValue());
2546 }
2547
2548 LValue CIRGenFunction::emitPredefinedLValue(const PredefinedExpr *e) {
2549 const StringLiteral *sl = e->getFunctionName();
2550 assert(sl != nullptr && "No StringLiteral name in PredefinedExpr");
2551 auto fn = cast<cir::FuncOp>(curFn);
2552 StringRef fnName = fn.getName();
2553 fnName.consume_front("\01");
2554 std::array<StringRef, 2> nameItems = {
2555 PredefinedExpr::getIdentKindName(e->getIdentKind()), fnName};
2556 std::string gvName = llvm::join(nameItems, ".");
2557 if (isa_and_nonnull<BlockDecl>(curCodeDecl))
2558 cgm.errorNYI(e->getSourceRange(), "predefined lvalue in block");
2559
2560 return emitStringLiteralLValue(sl, gvName);
2561 }
2562
2563 LValue CIRGenFunction::emitOpaqueValueLValue(const OpaqueValueExpr *e) {
2564 assert(OpaqueValueMappingData::shouldBindAsLValue(e));
2565 return getOrCreateOpaqueLValueMapping(e);
2566 }
2567
2568 namespace {
2569 // Handle the case where the condition is a constant evaluatable simple integer,
2570 // which means we don't have to separately handle the true/false blocks.
2571 std::optional<LValue> handleConditionalOperatorLValueSimpleCase(
2572 CIRGenFunction &cgf, const AbstractConditionalOperator *e) {
2573 const Expr *condExpr = e->getCond();
2574 llvm::APSInt condExprVal;
2575 if (!cgf.constantFoldsToSimpleInteger(condExpr, condExprVal))
2576 return std::nullopt;
2577
2578 const Expr *live = e->getTrueExpr(), *dead = e->getFalseExpr();
2579 if (!condExprVal.getBoolValue())
2580 std::swap(live, dead);
2581
2582 if (cgf.containsLabel(dead))
2583 return std::nullopt;
2584
2585 // If the true case is live, we need to track its region.
2586 assert(!cir::MissingFeatures::incrementProfileCounter());
2587 assert(!cir::MissingFeatures::pgoUse());
2588 // If a throw expression we emit it and return an undefined lvalue
2589 // because it can't be used.
2590 if (auto *throwExpr = dyn_cast<CXXThrowExpr>(live->IgnoreParens())) {
2591 cgf.emitCXXThrowExpr(throwExpr);
2592 // Return an undefined lvalue - the throw terminates execution
2593 // so this value will never actually be used
2594 mlir::Type elemTy = cgf.convertType(dead->getType());
2595 mlir::Value undefPtr =
2596 cgf.getBuilder().getNullPtr(cgf.getBuilder().getPointerTo(elemTy),
2597 cgf.getLoc(throwExpr->getSourceRange()));
2598 return cgf.makeAddrLValue(Address(undefPtr, elemTy, CharUnits::One()),
2599 dead->getType());
2600 }
2601 return cgf.emitLValue(live);
2602 }
2603
2604 /// Emit the operand of a glvalue conditional operator. This is either a glvalue
2605 /// or a (possibly-parenthesized) throw-expression. If this is a throw, no
2606 /// LValue is returned and the current block has been terminated.
2607 static std::optional<LValue> emitLValueOrThrowExpression(CIRGenFunction &cgf,
2608 const Expr *operand) {
2609 if (auto *throwExpr = dyn_cast<CXXThrowExpr>(operand->IgnoreParens())) {
2610 cgf.emitCXXThrowExpr(throwExpr);
2611 return std::nullopt;
2612 }
2613
2614 return cgf.emitLValue(operand);
2615 }
2616 } // namespace
2617
2618 // Create and generate the 3 blocks for a conditional operator.
2619 // Leaves the 'current block' in the continuation basic block.
2620 template <typename FuncTy>
2621 CIRGenFunction::ConditionalInfo
2622 CIRGenFunction::emitConditionalBlocks(const AbstractConditionalOperator *e,
2623 const FuncTy &branchGenFunc) {
2624 ConditionalInfo info;
2625 ConditionalEvaluation eval(*this);
2626 mlir::Location loc = getLoc(e->getSourceRange());
2627 CIRGenBuilderTy &builder = getBuilder();
2628
2629 mlir::Value condV = emitOpOnBoolExpr(loc, e->getCond());
2630 SmallVector<mlir::OpBuilder::InsertPoint, 2> insertPoints{};
2631 mlir::Type yieldTy{};
2632
2633 auto emitBranch = [&](mlir::OpBuilder &b, mlir::Location loc,
2634 const Expr *expr, std::optional<LValue> &resultLV) {
2635 CIRGenFunction::LexicalScope lexScope{*this, loc, b.getInsertionBlock()};
2636 curLexScope->setAsTernary();
2637
2638 assert(!cir::MissingFeatures::incrementProfileCounter());
2639 eval.beginEvaluation();
2640 resultLV = branchGenFunc(*this, expr);
2641 mlir::Value resultPtr = resultLV ? resultLV->getPointer() : mlir::Value();
2642 eval.endEvaluation();
2643
2644 if (resultPtr) {
2645 yieldTy = resultPtr.getType();
2646 cir::YieldOp::create(b, loc, resultPtr);
2647 } else {
2648 // If LHS or RHS is a void expression we need
2649 // to patch arms as to properly match yield types.
2650 // If the current block's terminator is an UnreachableOp (from a throw),
2651 // we don't need a yield
2652 if (builder.getInsertionBlock()->mightHaveTerminator()) {
2653 mlir::Operation *terminator =
2654 builder.getInsertionBlock()->getTerminator();
2655 if (isa_and_nonnull<cir::UnreachableOp>(terminator))
2656 insertPoints.push_back(b.saveInsertionPoint());
2657 }
2658 }
2659 };
2660
2661 info.result = cir::TernaryOp::create(
2662 builder, loc, condV,
2663 /*trueBuilder=*/
2664 [&](mlir::OpBuilder &b, mlir::Location loc) {
2665 emitBranch(b, loc, e->getTrueExpr(), info.lhs);
2666 },
2667 /*falseBuilder=*/
2668 [&](mlir::OpBuilder &b, mlir::Location loc) {
2669 emitBranch(b, loc, e->getFalseExpr(), info.rhs);
2670 })
2671 .getResult();
2672
2673 // If both arms are void, so be it.
2674 if (!yieldTy)
2675 yieldTy = voidTy;
2676
2677 // Insert required yields.
2678 for (mlir::OpBuilder::InsertPoint &toInsert : insertPoints) {
2679 mlir::OpBuilder::InsertionGuard guard(builder);
2680 builder.restoreInsertionPoint(toInsert);
2681
2682 // Block does not return: build empty yield.
2683 if (!yieldTy) {
2684 cir::YieldOp::create(builder, loc);
2685 } else { // Block returns: set null yield value.
2686 mlir::Value op0 = builder.getNullValue(yieldTy, loc);
2687 cir::YieldOp::create(builder, loc, op0);
2688 }
2689 }
2690
2691 return info;
2692 }
2693
2694 LValue CIRGenFunction::emitConditionalOperatorLValue(
2695 const AbstractConditionalOperator *expr) {
2696 if (!expr->isGLValue()) {
2697 // ?: here should be an aggregate.
2698 assert(hasAggregateEvaluationKind(expr->getType()) &&
2699 "Unexpected conditional operator!");
2700 return emitAggExprToLValue(expr);
2701 }
2702
2703 OpaqueValueMapping binding(*this, expr);
2704 if (std::optional<LValue> res =
2705 handleConditionalOperatorLValueSimpleCase(*this, expr))
2706 return *res;
2707
2708 ConditionalInfo info =
2709 emitConditionalBlocks(expr, [](CIRGenFunction &cgf, const Expr *e) {
2710 return emitLValueOrThrowExpression(cgf, e);
2711 });
2712
2713 if ((info.lhs && !info.lhs->isSimple()) ||
2714 (info.rhs && !info.rhs->isSimple())) {
2715 cgm.errorNYI(expr->getSourceRange(),
2716 "unsupported conditional operator with non-simple lvalue");
2717 return LValue();
2718 }
2719
2720 if (info.lhs && info.rhs) {
2721 Address lhsAddr = info.lhs->getAddress();
2722 Address rhsAddr = info.rhs->getAddress();
2723 Address result(info.result, lhsAddr.getElementType(),
2724 std::min(lhsAddr.getAlignment(), rhsAddr.getAlignment()));
2725 AlignmentSource alignSource =
2726 std::max(info.lhs->getBaseInfo().getAlignmentSource(),
2727 info.rhs->getBaseInfo().getAlignmentSource());
2728 assert(!cir::MissingFeatures::opTBAA());
2729 return makeAddrLValue(result, expr->getType(), LValueBaseInfo(alignSource));
2730 }
2731
2732 assert((info.lhs || info.rhs) &&
2733 "both operands of glvalue conditional are throw-expressions?");
2734 return info.lhs ? *info.lhs : *info.rhs;
2735 }
2736
2737 /// An LValue is a candidate for having its loads and stores be made atomic if
2738 /// we are operating under /volatile:ms *and* the LValue itself is volatile and
2739 /// performing such an operation can be performed without a libcall.
2740 bool CIRGenFunction::isLValueSuitableForInlineAtomic(LValue lv) {
2741 if (!cgm.getLangOpts().MSVolatile)
2742 return false;
2743
2744 cgm.errorNYI("LValueSuitableForInlineAtomic LangOpts MSVolatile");
2745 return false;
2746 }