clang 22.0.0git
CIRGenExprCXX.cpp
Go to the documentation of this file.
1//===--- CIRGenExprCXX.cpp - Emit CIR Code for C++ expressions ------------===//
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 dealing with code generation of C++ expressions
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenCXXABI.h"
15#include "CIRGenFunction.h"
16
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/ExprCXX.h"
20
21using namespace clang;
22using namespace clang::CIRGen;
23
24namespace {
25struct MemberCallInfo {
26 RequiredArgs reqArgs;
27 // Number of prefix arguments for the call. Ignores the `this` pointer.
28 unsigned prefixSize;
29};
30} // namespace
31
33 CIRGenFunction &cgf, const CXXMethodDecl *md, mlir::Value thisPtr,
34 mlir::Value implicitParam, QualType implicitParamTy, const CallExpr *ce,
35 CallArgList &args, CallArgList *rtlArgs) {
36 assert(ce == nullptr || isa<CXXMemberCallExpr>(ce) ||
38 assert(md->isInstance() &&
39 "Trying to emit a member or operator call expr on a static method!");
40
41 // Push the this ptr.
42 const CXXRecordDecl *rd =
44 args.add(RValue::get(thisPtr), cgf.getTypes().deriveThisType(rd, md));
45
46 // If there is an implicit parameter (e.g. VTT), emit it.
47 if (implicitParam) {
48 args.add(RValue::get(implicitParam), implicitParamTy);
49 }
50
51 const auto *fpt = md->getType()->castAs<FunctionProtoType>();
52 RequiredArgs required =
54 unsigned prefixSize = args.size() - 1;
55
56 // Add the rest of the call args
57 if (rtlArgs) {
58 // Special case: if the caller emitted the arguments right-to-left already
59 // (prior to emitting the *this argument), we're done. This happens for
60 // assignment operators.
61 args.addFrom(*rtlArgs);
62 } else if (ce) {
63 // Special case: skip first argument of CXXOperatorCall (it is "this").
64 unsigned argsToSkip = isa<CXXOperatorCallExpr>(ce) ? 1 : 0;
65 cgf.emitCallArgs(args, fpt, drop_begin(ce->arguments(), argsToSkip),
66 ce->getDirectCallee());
67 } else {
68 assert(
69 fpt->getNumParams() == 0 &&
70 "No CallExpr specified for function with non-zero number of arguments");
71 }
72
73 // return {required, prefixSize};
74 return {required, prefixSize};
75}
76
79 bool hasQualifier, NestedNameSpecifier qualifier, bool isArrow,
80 const Expr *base) {
82
83 // Compute the object pointer.
84 bool canUseVirtualCall = md->isVirtual() && !hasQualifier;
85 const CXXMethodDecl *devirtualizedMethod = nullptr;
87
88 // Note on trivial assignment
89 // --------------------------
90 // Classic codegen avoids generating the trivial copy/move assignment operator
91 // when it isn't necessary, choosing instead to just produce IR with an
92 // equivalent effect. We have chosen not to do that in CIR, instead emitting
93 // trivial copy/move assignment operators and allowing later transformations
94 // to optimize them away if appropriate.
95
96 // C++17 demands that we evaluate the RHS of a (possibly-compound) assignment
97 // operator before the LHS.
98 CallArgList rtlArgStorage;
99 CallArgList *rtlArgs = nullptr;
100 if (auto *oce = dyn_cast<CXXOperatorCallExpr>(ce)) {
101 if (oce->isAssignmentOp()) {
102 rtlArgs = &rtlArgStorage;
103 emitCallArgs(*rtlArgs, md->getType()->castAs<FunctionProtoType>(),
104 drop_begin(ce->arguments(), 1), ce->getDirectCallee(),
105 /*ParamsToSkip*/ 0);
106 }
107 }
108
109 LValue thisPtr;
110 if (isArrow) {
111 LValueBaseInfo baseInfo;
113 Address thisValue = emitPointerWithAlignment(base, &baseInfo);
114 thisPtr = makeAddrLValue(thisValue, base->getType(), baseInfo);
115 } else {
116 thisPtr = emitLValue(base);
117 }
118
119 if (isa<CXXConstructorDecl>(md)) {
120 cgm.errorNYI(ce->getSourceRange(),
121 "emitCXXMemberOrOperatorMemberCallExpr: constructor call");
122 return RValue::get(nullptr);
123 }
124
125 if ((md->isTrivial() || (md->isDefaulted() && md->getParent()->isUnion())) &&
127 return RValue::get(nullptr);
128
129 // Compute the function type we're calling
130 const CXXMethodDecl *calleeDecl =
131 devirtualizedMethod ? devirtualizedMethod : md;
132 const CIRGenFunctionInfo *fInfo = nullptr;
133 if (const auto *dtor = dyn_cast<CXXDestructorDecl>(calleeDecl))
134 fInfo = &cgm.getTypes().arrangeCXXStructorDeclaration(
136 else
137 fInfo = &cgm.getTypes().arrangeCXXMethodDeclaration(calleeDecl);
138
139 cir::FuncType ty = cgm.getTypes().getFunctionType(*fInfo);
140
143
144 // C++ [class.virtual]p12:
145 // Explicit qualification with the scope operator (5.1) suppresses the
146 // virtual call mechanism.
147 //
148 // We also don't emit a virtual call if the base expression has a record type
149 // because then we know what the type is.
150 bool useVirtualCall = canUseVirtualCall && !devirtualizedMethod;
151
152 if (const auto *dtor = dyn_cast<CXXDestructorDecl>(calleeDecl)) {
153 assert(ce->arg_begin() == ce->arg_end() &&
154 "Destructor shouldn't have explicit parameters");
155 assert(returnValue.isNull() && "Destructor shouldn't have return value");
156 if (useVirtualCall) {
157 cgm.getCXXABI().emitVirtualDestructorCall(*this, dtor, Dtor_Complete,
158 thisPtr.getAddress(),
160 } else {
161 GlobalDecl globalDecl(dtor, Dtor_Complete);
162 CIRGenCallee callee;
164 if (!devirtualizedMethod) {
166 cgm.getAddrOfCXXStructor(globalDecl, fInfo, ty), globalDecl);
167 } else {
168 cgm.errorNYI(ce->getSourceRange(), "devirtualized destructor call");
169 return RValue::get(nullptr);
170 }
171
172 QualType thisTy =
173 isArrow ? base->getType()->getPointeeType() : base->getType();
174 // CIRGen does not pass CallOrInvoke here (different from OG LLVM codegen)
175 // because in practice it always null even in OG.
176 emitCXXDestructorCall(globalDecl, callee, thisPtr.getPointer(), thisTy,
177 /*implicitParam=*/nullptr,
178 /*implicitParamTy=*/QualType(), ce);
179 }
180 return RValue::get(nullptr);
181 }
182
183 CIRGenCallee callee;
184 if (useVirtualCall) {
185 callee = CIRGenCallee::forVirtual(ce, md, thisPtr.getAddress(), ty);
186 } else {
188 if (getLangOpts().AppleKext) {
189 cgm.errorNYI(ce->getSourceRange(),
190 "emitCXXMemberOrOperatorMemberCallExpr: AppleKext");
191 return RValue::get(nullptr);
192 }
193
194 callee = CIRGenCallee::forDirect(cgm.getAddrOfFunction(calleeDecl, ty),
195 GlobalDecl(calleeDecl));
196 }
197
198 if (md->isVirtual()) {
199 Address newThisAddr =
200 cgm.getCXXABI().adjustThisArgumentForVirtualFunctionCall(
201 *this, calleeDecl, thisPtr.getAddress(), useVirtualCall);
202 thisPtr.setAddress(newThisAddr);
203 }
204
206 calleeDecl, callee, returnValue, thisPtr.getPointer(),
207 /*ImplicitParam=*/nullptr, QualType(), ce, rtlArgs);
208}
209
210RValue
212 const CXXMethodDecl *md,
214 assert(md->isInstance() &&
215 "Trying to emit a member call expr on a static method!");
217 e, md, returnValue, /*HasQualifier=*/false, /*Qualifier=*/std::nullopt,
218 /*IsArrow=*/false, e->getArg(0));
219}
220
222 const CXXMethodDecl *md, const CIRGenCallee &callee,
223 ReturnValueSlot returnValue, mlir::Value thisPtr, mlir::Value implicitParam,
224 QualType implicitParamTy, const CallExpr *ce, CallArgList *rtlArgs) {
225 const auto *fpt = md->getType()->castAs<FunctionProtoType>();
226 CallArgList args;
227 MemberCallInfo callInfo = commonBuildCXXMemberOrOperatorCall(
228 *this, md, thisPtr, implicitParam, implicitParamTy, ce, args, rtlArgs);
229 auto &fnInfo = cgm.getTypes().arrangeCXXMethodCall(
230 args, fpt, callInfo.reqArgs, callInfo.prefixSize);
231 assert((ce || currSrcLoc) && "expected source location");
232 mlir::Location loc = ce ? getLoc(ce->getExprLoc()) : *currSrcLoc;
234 return emitCall(fnInfo, callee, returnValue, args, nullptr, loc);
235}
236
238 const CXXNewExpr *e) {
239 if (!e->isArray())
240 return CharUnits::Zero();
241
242 // No cookie is required if the operator new[] being used is the
243 // reserved placement operator new[].
245 return CharUnits::Zero();
246
247 return cgf.cgm.getCXXABI().getArrayCookieSize(e);
248}
249
250static mlir::Value emitCXXNewAllocSize(CIRGenFunction &cgf, const CXXNewExpr *e,
251 unsigned minElements,
252 mlir::Value &numElements,
253 mlir::Value &sizeWithoutCookie) {
255 mlir::Location loc = cgf.getLoc(e->getSourceRange());
256
257 if (!e->isArray()) {
259 sizeWithoutCookie = cgf.getBuilder().getConstant(
260 loc, cir::IntAttr::get(cgf.SizeTy, typeSize.getQuantity()));
261 return sizeWithoutCookie;
262 }
263
264 // The width of size_t.
265 unsigned sizeWidth = cgf.cgm.getDataLayout().getTypeSizeInBits(cgf.SizeTy);
266
267 // The number of elements can be have an arbitrary integer type;
268 // essentially, we need to multiply it by a constant factor, add a
269 // cookie size, and verify that the result is representable as a
270 // size_t. That's just a gloss, though, and it's wrong in one
271 // important way: if the count is negative, it's an error even if
272 // the cookie size would bring the total size >= 0.
273 //
274 // If the array size is constant, Sema will have prevented negative
275 // values and size overflow.
276
277 // Compute the constant factor.
278 llvm::APInt arraySizeMultiplier(sizeWidth, 1);
279 while (const ConstantArrayType *cat =
281 type = cat->getElementType();
282 arraySizeMultiplier *= cat->getSize();
283 }
284
286 llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity());
287 typeSizeMultiplier *= arraySizeMultiplier;
288
289 // Figure out the cookie size.
290 llvm::APInt cookieSize(sizeWidth,
291 calculateCookiePadding(cgf, e).getQuantity());
292
293 // This will be a size_t.
294 mlir::Value size;
295
296 // Emit the array size expression.
297 // We multiply the size of all dimensions for NumElements.
298 // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
299 const Expr *arraySize = *e->getArraySize();
300 mlir::Attribute constNumElements =
301 ConstantEmitter(cgf.cgm, &cgf)
302 .emitAbstract(arraySize, arraySize->getType());
303 if (constNumElements) {
304 // Get an APInt from the constant
305 const llvm::APInt &count =
306 mlir::cast<cir::IntAttr>(constNumElements).getValue();
307
308 unsigned numElementsWidth = count.getBitWidth();
309
310 // The equivalent code in CodeGen/CGExprCXX.cpp handles these cases as
311 // overflow, but that should never happen. The size argument is implicitly
312 // cast to a size_t, so it can never be negative and numElementsWidth will
313 // always equal sizeWidth.
314 assert(!count.isNegative() && "Expected non-negative array size");
315 assert(numElementsWidth == sizeWidth &&
316 "Expected a size_t array size constant");
317
318 // Okay, compute a count at the right width.
319 llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth);
320
321 // Scale numElements by that. This might overflow, but we don't
322 // care because it only overflows if allocationSize does too, and
323 // if that overflows then we shouldn't use this.
324 // This emits a constant that may not be used, but we can't tell here
325 // whether it will be needed or not.
326 numElements =
327 cgf.getBuilder().getConstInt(loc, adjustedCount * arraySizeMultiplier);
328
329 // Compute the size before cookie, and track whether it overflowed.
330 bool overflow;
331 llvm::APInt allocationSize =
332 adjustedCount.umul_ov(typeSizeMultiplier, overflow);
333
334 // Sema prevents us from hitting this case
335 assert(!overflow && "Overflow in array allocation size");
336
337 // Add in the cookie, and check whether it's overflowed.
338 if (cookieSize != 0) {
339 cgf.cgm.errorNYI(e->getSourceRange(),
340 "emitCXXNewAllocSize: array cookie");
341 }
342
343 size = cgf.getBuilder().getConstInt(loc, allocationSize);
344 } else {
345 // TODO: Handle the variable size case
346 cgf.cgm.errorNYI(e->getSourceRange(),
347 "emitCXXNewAllocSize: variable array size");
348 }
349
350 if (cookieSize == 0)
351 sizeWithoutCookie = size;
352 else
353 assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?");
354
355 return size;
356}
357
358static void storeAnyExprIntoOneUnit(CIRGenFunction &cgf, const Expr *init,
359 QualType allocType, Address newPtr,
360 AggValueSlot::Overlap_t mayOverlap) {
361 // FIXME: Refactor with emitExprAsInit.
362 switch (cgf.getEvaluationKind(allocType)) {
363 case cir::TEK_Scalar:
364 cgf.emitScalarInit(init, cgf.getLoc(init->getSourceRange()),
365 cgf.makeAddrLValue(newPtr, allocType), false);
366 return;
367 case cir::TEK_Complex:
368 cgf.emitComplexExprIntoLValue(init, cgf.makeAddrLValue(newPtr, allocType),
369 /*isInit*/ true);
370 return;
371 case cir::TEK_Aggregate: {
375 newPtr, allocType.getQualifiers(), AggValueSlot::IsDestructed,
377 cgf.emitAggExpr(init, slot);
378 return;
379 }
380 }
381 llvm_unreachable("bad evaluation kind");
382}
383
385 const CXXNewExpr *e, QualType elementType, mlir::Type elementTy,
386 Address beginPtr, mlir::Value numElements,
387 mlir::Value allocSizeWithoutCookie) {
388 // If we have a type with trivial initialization and no initializer,
389 // there's nothing to do.
390 if (!e->hasInitializer())
391 return;
392
393 cgm.errorNYI(e->getSourceRange(), "emitNewArrayInitializer");
394}
395
397 QualType elementType, mlir::Type elementTy,
398 Address newPtr, mlir::Value numElements,
399 mlir::Value allocSizeWithoutCookie) {
401 if (e->isArray()) {
402 cgf.emitNewArrayInitializer(e, elementType, elementTy, newPtr, numElements,
403 allocSizeWithoutCookie);
404 } else if (const Expr *init = e->getInitializer()) {
405 storeAnyExprIntoOneUnit(cgf, init, e->getAllocatedType(), newPtr,
407 }
408}
409
411 GlobalDecl dtor, const CIRGenCallee &callee, mlir::Value thisVal,
412 QualType thisTy, mlir::Value implicitParam, QualType implicitParamTy,
413 const CallExpr *ce) {
414 const CXXMethodDecl *dtorDecl = cast<CXXMethodDecl>(dtor.getDecl());
415
416 assert(!thisTy.isNull());
417 assert(thisTy->getAsCXXRecordDecl() == dtorDecl->getParent() &&
418 "Pointer/Object mixup");
419
421
422 CallArgList args;
423 commonBuildCXXMemberOrOperatorCall(*this, dtorDecl, thisVal, implicitParam,
424 implicitParamTy, ce, args, nullptr);
425 assert((ce || dtor.getDecl()) && "expected source location provider");
427 return emitCall(cgm.getTypes().arrangeCXXStructorDeclaration(dtor), callee,
428 ReturnValueSlot(), args, nullptr,
429 ce ? getLoc(ce->getExprLoc())
430 : getLoc(dtor.getDecl()->getSourceRange()));
431}
432
435 QualType destroyedType = expr->getDestroyedType();
436 if (destroyedType.hasStrongOrWeakObjCLifetime()) {
438 cgm.errorNYI(expr->getExprLoc(),
439 "emitCXXPseudoDestructorExpr: Objective-C lifetime is NYI");
440 } else {
441 // C++ [expr.pseudo]p1:
442 // The result shall only be used as the operand for the function call
443 // operator (), and the result of such a call has type void. The only
444 // effect is the evaluation of the postfix-expression before the dot or
445 // arrow.
446 emitIgnoredExpr(expr->getBase());
447 }
448
449 return RValue::get(nullptr);
450}
451
452/// Emit a call to an operator new or operator delete function, as implicitly
453/// created by new-expressions and delete-expressions.
455 const FunctionDecl *calleeDecl,
456 const FunctionProtoType *calleeType,
457 const CallArgList &args) {
458 cir::CIRCallOpInterface callOrTryCall;
459 cir::FuncOp calleePtr = cgf.cgm.getAddrOfFunction(calleeDecl);
460 CIRGenCallee callee =
461 CIRGenCallee::forDirect(calleePtr, GlobalDecl(calleeDecl));
462 RValue rv =
463 cgf.emitCall(cgf.cgm.getTypes().arrangeFreeFunctionCall(args, calleeType),
464 callee, ReturnValueSlot(), args, &callOrTryCall);
465
466 /// C++1y [expr.new]p10:
467 /// [In a new-expression,] an implementation is allowed to omit a call
468 /// to a replaceable global allocation function.
469 ///
470 /// We model such elidable calls with the 'builtin' attribute.
472 return rv;
473}
474
475namespace {
476/// Calls the given 'operator delete' on a single object.
477struct CallObjectDelete final : EHScopeStack::Cleanup {
478 mlir::Value ptr;
479 const FunctionDecl *operatorDelete;
480 QualType elementType;
481
482 CallObjectDelete(mlir::Value ptr, const FunctionDecl *operatorDelete,
483 QualType elementType)
484 : ptr(ptr), operatorDelete(operatorDelete), elementType(elementType) {}
485
486 void emit(CIRGenFunction &cgf) override {
487 cgf.emitDeleteCall(operatorDelete, ptr, elementType);
488 }
489};
490} // namespace
491
492/// Emit the code for deleting a single object.
494 Address ptr, QualType elementType) {
495 // C++11 [expr.delete]p3:
496 // If the static type of the object to be deleted is different from its
497 // dynamic type, the static type shall be a base class of the dynamic type
498 // of the object to be deleted and the static type shall have a virtual
499 // destructor or the behavior is undefined.
501
502 const FunctionDecl *operatorDelete = de->getOperatorDelete();
503 assert(!operatorDelete->isDestroyingOperatorDelete());
504
505 // Find the destructor for the type, if applicable. If the
506 // destructor is virtual, we'll just emit the vcall and return.
507 const CXXDestructorDecl *dtor = nullptr;
508 if (const auto *rd = elementType->getAsCXXRecordDecl()) {
509 if (rd->hasDefinition() && !rd->hasTrivialDestructor()) {
510 dtor = rd->getDestructor();
511
512 if (dtor->isVirtual()) {
513 cgf.cgm.errorNYI(de->getSourceRange(),
514 "emitObjectDelete: virtual destructor");
515 }
516 }
517 }
518
519 // Make sure that we call delete even if the dtor throws.
520 // This doesn't have to a conditional cleanup because we're going
521 // to pop it off in a second.
522 cgf.ehStack.pushCleanup<CallObjectDelete>(
523 NormalAndEHCleanup, ptr.getPointer(), operatorDelete, elementType);
524
525 if (dtor) {
527 /*ForVirtualBase=*/false,
528 /*Delegating=*/false, ptr, elementType);
529 } else if (elementType.getObjCLifetime()) {
531 cgf.cgm.errorNYI(de->getSourceRange(), "emitObjectDelete: ObjCLifetime");
532 }
533
534 // In traditional LLVM codegen null checks are emitted to save a delete call.
535 // In CIR we optimize for size by default, the null check should be added into
536 // this function callers.
538
539 cgf.popCleanupBlock();
540}
541
543 const Expr *arg = e->getArgument();
545
546 // Null check the pointer.
547 //
548 // We could avoid this null check if we can determine that the object
549 // destruction is trivial and doesn't require an array cookie; we can
550 // unconditionally perform the operator delete call in that case. For now, we
551 // assume that deleted pointers are null rarely enough that it's better to
552 // keep the branch. This might be worth revisiting for a -O0 code size win.
553 //
554 // CIR note: emit the code size friendly by default for now, such as mentioned
555 // in `emitObjectDelete`.
557 QualType deleteTy = e->getDestroyedType();
558
559 // A destroying operator delete overrides the entire operation of the
560 // delete expression.
562 cgm.errorNYI(e->getSourceRange(),
563 "emitCXXDeleteExpr: destroying operator delete");
564 return;
565 }
566
567 // We might be deleting a pointer to array.
568 deleteTy = getContext().getBaseElementType(deleteTy);
569 ptr = ptr.withElementType(builder, convertTypeForMem(deleteTy));
570
571 if (e->isArrayForm()) {
573 cgm.errorNYI(e->getSourceRange(), "emitCXXDeleteExpr: array delete");
574 return;
575 } else {
576 emitObjectDelete(*this, e, ptr, deleteTy);
577 }
578}
579
581 // The element type being allocated.
583
584 // 1. Build a call to the allocation function.
585 FunctionDecl *allocator = e->getOperatorNew();
586
587 // If there is a brace-initializer, cannot allocate fewer elements than inits.
588 unsigned minElements = 0;
589 if (e->isArray() && e->hasInitializer()) {
590 cgm.errorNYI(e->getSourceRange(), "emitCXXNewExpr: array initializer");
591 }
592
593 mlir::Value numElements = nullptr;
594 mlir::Value allocSizeWithoutCookie = nullptr;
595 mlir::Value allocSize = emitCXXNewAllocSize(
596 *this, e, minElements, numElements, allocSizeWithoutCookie);
597 CharUnits allocAlign = getContext().getTypeAlignInChars(allocType);
598
599 // Emit the allocation call.
600 Address allocation = Address::invalid();
601 CallArgList allocatorArgs;
602 if (allocator->isReservedGlobalPlacementOperator()) {
603 cgm.errorNYI(e->getSourceRange(),
604 "emitCXXNewExpr: reserved global placement operator");
605 } else {
606 const FunctionProtoType *allocatorType =
607 allocator->getType()->castAs<FunctionProtoType>();
608 unsigned paramsToSkip = 0;
609
610 // The allocation size is the first argument.
611 QualType sizeType = getContext().getSizeType();
612 allocatorArgs.add(RValue::get(allocSize), sizeType);
613 ++paramsToSkip;
614
615 if (allocSize != allocSizeWithoutCookie) {
616 CharUnits cookieAlign = getSizeAlign(); // FIXME: Ask the ABI.
617 allocAlign = std::max(allocAlign, cookieAlign);
618 }
619
620 // The allocation alignment may be passed as the second argument.
621 if (e->passAlignment()) {
622 cgm.errorNYI(e->getSourceRange(), "emitCXXNewExpr: pass alignment");
623 }
624
625 // FIXME: Why do we not pass a CalleeDecl here?
626 emitCallArgs(allocatorArgs, allocatorType, e->placement_arguments(),
627 AbstractCallee(), paramsToSkip);
628 RValue rv =
629 emitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs);
630
631 // Set !heapallocsite metadata on the call to operator new.
633
634 // If this was a call to a global replaceable allocation function that does
635 // not take an alignment argument, the allocator is known to produce storage
636 // that's suitably aligned for any object that fits, up to a known
637 // threshold. Otherwise assume it's suitably aligned for the allocated type.
638 CharUnits allocationAlign = allocAlign;
639 if (!e->passAlignment() &&
640 allocator->isReplaceableGlobalAllocationFunction()) {
641 const TargetInfo &target = cgm.getASTContext().getTargetInfo();
642 unsigned allocatorAlign = llvm::bit_floor(std::min<uint64_t>(
643 target.getNewAlign(), getContext().getTypeSize(allocType)));
644 allocationAlign = std::max(
645 allocationAlign, getContext().toCharUnitsFromBits(allocatorAlign));
646 }
647
648 mlir::Value allocPtr = rv.getValue();
649 allocation = Address(
650 allocPtr, mlir::cast<cir::PointerType>(allocPtr.getType()).getPointee(),
651 allocationAlign);
652 }
653
654 // Emit a null check on the allocation result if the allocation
655 // function is allowed to return null (because it has a non-throwing
656 // exception spec or is the reserved placement new) and we have an
657 // interesting initializer will be running sanitizers on the initialization.
658 bool nullCheck = e->shouldNullCheckAllocation() &&
659 (!allocType.isPODType(getContext()) || e->hasInitializer());
661 if (nullCheck)
662 cgm.errorNYI(e->getSourceRange(), "emitCXXNewExpr: null check");
663
664 // If there's an operator delete, enter a cleanup to call it if an
665 // exception is thrown.
666 if (e->getOperatorDelete() &&
668 cgm.errorNYI(e->getSourceRange(), "emitCXXNewExpr: operator delete");
669
670 if (allocSize != allocSizeWithoutCookie)
671 cgm.errorNYI(e->getSourceRange(), "emitCXXNewExpr: array with cookies");
672
673 mlir::Type elementTy;
674 if (e->isArray()) {
675 // For array new, use the allocated type to handle multidimensional arrays
676 // correctly
677 elementTy = convertTypeForMem(e->getAllocatedType());
678 } else {
679 elementTy = convertTypeForMem(allocType);
680 }
681 Address result = builder.createElementBitCast(getLoc(e->getSourceRange()),
682 allocation, elementTy);
683
684 // Passing pointer through launder.invariant.group to avoid propagation of
685 // vptrs information which may be included in previous type.
686 // To not break LTO with different optimizations levels, we do it regardless
687 // of optimization level.
688 if (cgm.getCodeGenOpts().StrictVTablePointers &&
689 allocator->isReservedGlobalPlacementOperator())
690 cgm.errorNYI(e->getSourceRange(), "emitCXXNewExpr: strict vtable pointers");
691
693
694 emitNewInitializer(*this, e, allocType, elementTy, result, numElements,
695 allocSizeWithoutCookie);
696 return result.getPointer();
697}
698
700 mlir::Value ptr, QualType deleteTy) {
702
703 const auto *deleteFTy = deleteFD->getType()->castAs<FunctionProtoType>();
704 CallArgList deleteArgs;
705
706 UsualDeleteParams params = deleteFD->getUsualDeleteParams();
707 auto paramTypeIt = deleteFTy->param_type_begin();
708
709 // Pass std::type_identity tag if present
711 cgm.errorNYI(deleteFD->getSourceRange(),
712 "emitDeleteCall: type aware delete");
713
714 // Pass the pointer itself.
715 QualType argTy = *paramTypeIt++;
716 mlir::Value deletePtr =
717 builder.createBitcast(ptr.getLoc(), ptr, convertType(argTy));
718 deleteArgs.add(RValue::get(deletePtr), argTy);
719
720 // Pass the std::destroying_delete tag if present.
721 if (params.DestroyingDelete)
722 cgm.errorNYI(deleteFD->getSourceRange(),
723 "emitDeleteCall: destroying delete");
724
725 // Pass the size if the delete function has a size_t parameter.
726 if (params.Size) {
727 QualType sizeType = *paramTypeIt++;
728 CharUnits deleteTypeSize = getContext().getTypeSizeInChars(deleteTy);
729 assert(mlir::isa<cir::IntType>(convertType(sizeType)) &&
730 "expected cir::IntType");
731 cir::ConstantOp size = builder.getConstInt(
732 *currSrcLoc, convertType(sizeType), deleteTypeSize.getQuantity());
733
734 deleteArgs.add(RValue::get(size), sizeType);
735 }
736
737 // Pass the alignment if the delete function has an align_val_t parameter.
738 if (isAlignedAllocation(params.Alignment))
739 cgm.errorNYI(deleteFD->getSourceRange(),
740 "emitDeleteCall: aligned allocation");
741
742 assert(paramTypeIt == deleteFTy->param_type_end() &&
743 "unknown parameter to usual delete function");
744
745 // Emit the call to delete.
746 emitNewDeleteCall(*this, deleteFD, deleteFTy, deleteArgs);
747}
748
750 const CXXDynamicCastExpr *dce) {
751 mlir::Location loc = getLoc(dce->getSourceRange());
752
753 cgm.emitExplicitCastExprType(dce, this);
754 QualType destTy = dce->getTypeAsWritten();
755 QualType srcTy = dce->getSubExpr()->getType();
756
757 // C++ [expr.dynamic.cast]p7:
758 // If T is "pointer to cv void," then the result is a pointer to the most
759 // derived object pointed to by v.
760 bool isDynCastToVoid = destTy->isVoidPointerType();
761 bool isRefCast = destTy->isReferenceType();
762
763 QualType srcRecordTy;
764 QualType destRecordTy;
765 if (isDynCastToVoid) {
766 srcRecordTy = srcTy->getPointeeType();
767 // No destRecordTy.
768 } else if (const PointerType *destPTy = destTy->getAs<PointerType>()) {
769 srcRecordTy = srcTy->castAs<PointerType>()->getPointeeType();
770 destRecordTy = destPTy->getPointeeType();
771 } else {
772 srcRecordTy = srcTy;
773 destRecordTy = destTy->castAs<ReferenceType>()->getPointeeType();
774 }
775
776 assert(srcRecordTy->isRecordType() && "source type must be a record type!");
778
779 if (dce->isAlwaysNull()) {
780 cgm.errorNYI(dce->getSourceRange(), "emitDynamicCastToNull");
781 return {};
782 }
783
784 auto destCirTy = mlir::cast<cir::PointerType>(convertType(destTy));
785 return cgm.getCXXABI().emitDynamicCast(*this, loc, srcRecordTy, destRecordTy,
786 destCirTy, isRefCast, thisAddr);
787}
static void emit(Program &P, llvm::SmallVectorImpl< std::byte > &Code, const T &Val, bool &Success)
Helper to write bytecode and bail out if 32-bit offsets become invalid.
static void emitNewInitializer(CIRGenFunction &cgf, const CXXNewExpr *e, QualType elementType, mlir::Type elementTy, Address newPtr, mlir::Value numElements, mlir::Value allocSizeWithoutCookie)
static void emitObjectDelete(CIRGenFunction &cgf, const CXXDeleteExpr *de, Address ptr, QualType elementType)
Emit the code for deleting a single object.
static mlir::Value emitCXXNewAllocSize(CIRGenFunction &cgf, const CXXNewExpr *e, unsigned minElements, mlir::Value &numElements, mlir::Value &sizeWithoutCookie)
static void storeAnyExprIntoOneUnit(CIRGenFunction &cgf, const Expr *init, QualType allocType, Address newPtr, AggValueSlot::Overlap_t mayOverlap)
static CharUnits calculateCookiePadding(CIRGenFunction &cgf, const CXXNewExpr *e)
static MemberCallInfo commonBuildCXXMemberOrOperatorCall(CIRGenFunction &cgf, const CXXMethodDecl *md, mlir::Value thisPtr, mlir::Value implicitParam, QualType implicitParamTy, const CallExpr *ce, CallArgList &args, CallArgList *rtlArgs)
static RValue emitNewDeleteCall(CIRGenFunction &cgf, const FunctionDecl *calleeDecl, const FunctionProtoType *calleeType, const CallArgList &args)
Emit a call to an operator new or operator delete function, as implicitly created by new-expressions ...
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
static QualType getPointeeType(const MemRegion *R)
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr)
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const
const ConstantArrayType * getAsConstantArrayType(QualType T) const
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
mlir::Value getPointer() const
Definition Address.h:82
static Address invalid()
Definition Address.h:67
Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const
Return address with different element type, a bitcast pointer, and the same alignment.
An aggregate value slot.
static AggValueSlot forAddr(Address addr, clang::Qualifiers quals, IsDestructed_t isDestructed, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed)
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
virtual const clang::CXXRecordDecl * getThisArgumentTypeForMethod(const clang::CXXMethodDecl *md)
Get the type of the implicit "this" parameter used by a method.
virtual CharUnits getArrayCookieSize(const CXXNewExpr *e)
Returns the extra size required in order to store the array cookie for the given new-expression.
static CIRGenCallee forDirect(mlir::Operation *funcPtr, const CIRGenCalleeInfo &abstractInfo=CIRGenCalleeInfo())
Definition CIRGenCall.h:90
static CIRGenCallee forVirtual(const clang::CallExpr *ce, clang::GlobalDecl md, Address addr, cir::FuncType fTy)
Definition CIRGenCall.h:152
An abstract representation of regular/ObjC call/message targets.
void emitCallArgs(CallArgList &args, PrototypeWrapper prototype, llvm::iterator_range< clang::CallExpr::const_arg_iterator > argRange, AbstractCallee callee=AbstractCallee(), unsigned paramsToSkip=0)
mlir::Type convertType(clang::QualType t)
static cir::TypeEvaluationKind getEvaluationKind(clang::QualType type)
Return the cir::TypeEvaluationKind of QualType type.
CIRGenTypes & getTypes() const
Address emitPointerWithAlignment(const clang::Expr *expr, LValueBaseInfo *baseInfo=nullptr)
Given an expression with a pointer type, emit the value and compute our best estimate of the alignmen...
const clang::LangOptions & getLangOpts() const
void emitDeleteCall(const FunctionDecl *deleteFD, mlir::Value ptr, QualType deleteTy)
LValue emitLValue(const clang::Expr *e)
Emit code to compute a designator that specifies the location of the expression.
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
RValue emitCXXMemberOrOperatorCall(const clang::CXXMethodDecl *md, const CIRGenCallee &callee, ReturnValueSlot returnValue, mlir::Value thisPtr, mlir::Value implicitParam, clang::QualType implicitParamTy, const clang::CallExpr *ce, CallArgList *rtlArgs)
EHScopeStack ehStack
Tracks function scope overall cleanup handling.
mlir::Type convertTypeForMem(QualType t)
mlir::Value emitCXXNewExpr(const CXXNewExpr *e)
Address returnValue
The temporary alloca to hold the return value.
void emitScalarInit(const clang::Expr *init, mlir::Location loc, LValue lvalue, bool capturedByInit=false)
RValue emitCall(const CIRGenFunctionInfo &funcInfo, const CIRGenCallee &callee, ReturnValueSlot returnValue, const CallArgList &args, cir::CIRCallOpInterface *callOp, mlir::Location loc)
void emitNewArrayInitializer(const CXXNewExpr *E, QualType ElementType, mlir::Type ElementTy, Address BeginPtr, mlir::Value NumElements, mlir::Value AllocSizeWithoutCookie)
CIRGenBuilderTy & getBuilder()
void emitComplexExprIntoLValue(const Expr *e, LValue dest, bool isInit)
LValue makeAddrLValue(Address addr, QualType ty, AlignmentSource source=AlignmentSource::Type)
void emitCXXDestructorCall(const CXXDestructorDecl *dd, CXXDtorType type, bool forVirtualBase, bool delegating, Address thisAddr, QualType thisTy)
std::optional< mlir::Location > currSrcLoc
Use to track source locations across nested visitor traversals.
clang::ASTContext & getContext() const
RValue emitCXXMemberOrOperatorMemberCallExpr(const clang::CallExpr *ce, const clang::CXXMethodDecl *md, ReturnValueSlot returnValue, bool hasQualifier, clang::NestedNameSpecifier qualifier, bool isArrow, const clang::Expr *base)
void emitCXXDeleteExpr(const CXXDeleteExpr *e)
RValue emitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *e, const CXXMethodDecl *md, ReturnValueSlot returnValue)
void emitIgnoredExpr(const clang::Expr *e)
Emit code to compute the specified expression, ignoring the result.
mlir::Value emitDynamicCast(Address thisAddr, const CXXDynamicCastExpr *dce)
void emitAggExpr(const clang::Expr *e, AggValueSlot slot)
void popCleanupBlock()
Pops a cleanup block.
RValue emitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *expr)
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
cir::FuncOp getAddrOfFunction(clang::GlobalDecl gd, mlir::Type funcType=nullptr, bool forVTable=false, bool dontDefer=false, ForDefinition_t isForDefinition=NotForDefinition)
Return the address of the given function.
const cir::CIRDataLayout getDataLayout() const
CIRGenCXXABI & getCXXABI() const
const CIRGenFunctionInfo & arrangeFreeFunctionCall(const CallArgList &args, const FunctionType *fnType)
clang::CanQualType deriveThisType(const clang::CXXRecordDecl *rd, const clang::CXXMethodDecl *md)
Derives the 'this' type for CIRGen purposes, i.e.
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition CIRGenCall.h:242
void add(RValue rvalue, clang::QualType type)
Definition CIRGenCall.h:233
mlir::Attribute emitAbstract(const Expr *e, QualType destType)
Emit the result of the given expression as an abstract constant, asserting that it succeeded.
Information for lazily generating a cleanup.
Address getAddress() const
mlir::Value getPointer() const
void setAddress(Address address)
This trivial value class is used to represent the result of an expression that is evaluated.
Definition CIRGenValue.h:33
static RValue get(mlir::Value v)
Definition CIRGenValue.h:82
mlir::Value getValue() const
Return the value of this scalar value.
Definition CIRGenValue.h:56
A class for recording the number of arguments that a function signature requires.
static RequiredArgs getFromProtoWithExtraSlots(const clang::FunctionProtoType *prototype, unsigned additional)
Compute the arguments required by the given formal prototype, given that there may be some additional...
Contains the address where the return value of a function can be stored, and whether the address is v...
Definition CIRGenCall.h:254
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition ExprCXX.h:2628
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2667
bool isArrayForm() const
Definition ExprCXX.h:2654
QualType getDestroyedType() const
Retrieve the type being destroyed.
Definition ExprCXX.cpp:338
Represents a C++ destructor within a class.
Definition DeclCXX.h:2869
A C++ dynamic_cast expression (C++ [expr.dynamic.cast]).
Definition ExprCXX.h:481
bool isAlwaysNull() const
isAlwaysNull - Return whether the result of the dynamic_cast is proven to always be null.
Definition ExprCXX.cpp:838
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
bool isVirtual() const
Definition DeclCXX.h:2184
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2255
bool isInstance() const
Definition DeclCXX.h:2156
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition ExprCXX.h:2357
bool isArray() const
Definition ExprCXX.h:2466
llvm::iterator_range< arg_iterator > placement_arguments()
Definition ExprCXX.h:2574
QualType getAllocatedType() const
Definition ExprCXX.h:2436
std::optional< Expr * > getArraySize()
This might return std::nullopt even if isArray() returns true, since there might not be an array size...
Definition ExprCXX.h:2471
bool hasInitializer() const
Whether this new-expression has any initializer at all.
Definition ExprCXX.h:2526
bool shouldNullCheckAllocation() const
True if the allocation result needs to be null-checked.
Definition ExprCXX.cpp:326
bool passAlignment() const
Indicates whether the required alignment should be implicitly passed to the allocation function.
Definition ExprCXX.h:2553
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2463
SourceRange getSourceRange() const
Definition ExprCXX.h:2612
FunctionDecl * getOperatorNew() const
Definition ExprCXX.h:2461
Expr * getInitializer()
The initializer of this new-expression.
Definition ExprCXX.h:2535
A call to an overloaded operator written using operator syntax.
Definition ExprCXX.h:84
Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
Definition ExprCXX.h:2747
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2877
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3081
arg_iterator arg_begin()
Definition Expr.h:3134
arg_iterator arg_end()
Definition Expr.h:3137
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3060
arg_range arguments()
Definition Expr.h:3129
Expr * getSubExpr()
Definition Expr.h:3660
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition CharUnits.h:53
Represents the canonical version of C arrays with a specified constant size.
Definition TypeBase.h:3760
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
Definition DeclBase.h:427
QualType getTypeAsWritten() const
getTypeAsWritten - Returns the type that this expression is casting to, as written in the source code...
Definition Expr.h:3889
This represents one expression.
Definition Expr.h:112
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:273
QualType getType() const
Definition Expr.h:144
Represents a function declaration or definition.
Definition Decl.h:2000
bool isDestroyingOperatorDelete() const
Determine whether this is a destroying operator delete.
Definition Decl.cpp:3543
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition Decl.h:2377
UsualDeleteParams getUsualDeleteParams() const
Definition Decl.cpp:3559
bool isReservedGlobalPlacementOperator() const
Determines whether this operator new or delete is one of the reserved global placement operators: voi...
Definition Decl.cpp:3395
bool isDefaulted() const
Whether this function is defaulted.
Definition Decl.h:2385
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:4541
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5266
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
const Decl * getDecl() const
Definition GlobalDecl.h:106
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3328
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8330
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition TypeBase.h:1438
bool isPODType(const ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition Type.cpp:2695
bool hasStrongOrWeakObjCLifetime() const
Definition TypeBase.h:1446
Base for LValueReferenceType and RValueReferenceType.
Definition TypeBase.h:3573
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:334
bool isUnion() const
Definition Decl.h:3922
Exposes information about the current target.
Definition TargetInfo.h:226
unsigned getNewAlign() const
Return the largest alignment for which a suitably-sized allocation with 'operator new(size_t)' is gua...
Definition TargetInfo.h:761
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isVoidPointerType() const
Definition Type.cpp:712
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9170
bool isReferenceType() const
Definition TypeBase.h:8551
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:752
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9103
bool isRecordType() const
Definition TypeBase.h:8654
QualType getType() const
Definition Decl.h:723
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool isAlignedAllocation(AlignedAllocationMode Mode)
Definition ExprCXX.h:2267
@ Dtor_Complete
Complete object dtor.
Definition ABI.h:36
bool isTypeAwareAllocation(TypeAwareAllocationMode Mode)
Definition ExprCXX.h:2255
U cast(CodeGen::Address addr)
Definition Address.h:327
static bool objCLifetime()
static bool addressSpace()
static bool aggValueSlotGC()
static bool devirtualizeMemberFunction()
static bool deleteArray()
static bool emitTypeCheck()
static bool opCallMustTail()
static bool exprNewNullCheck()
static bool attributeBuiltin()
static bool emitNullCheckForDeleteCalls()
static bool generateDebugInfo()
clang::CharUnits getSizeAlign() const
The parameters to pass to a usual operator delete.
Definition ExprCXX.h:2346
TypeAwareAllocationMode TypeAwareDelete
Definition ExprCXX.h:2347
AlignedAllocationMode Alignment
Definition ExprCXX.h:2350