25#include "mlir/IR/ImplicitLocOpBuilder.h"
27#include "llvm/Support/ErrorHandling.h"
33class LowerItaniumCXXABI :
public CIRCXXABI {
35 bool useARMMethodPtrABI;
36 bool use32BitVTableOffsetABI;
39 LowerItaniumCXXABI(LowerModule &lm,
bool useARMMethodPtrABI =
false,
40 bool use32BitVTableOffsetABI =
false)
41 : CIRCXXABI(lm), useARMMethodPtrABI(useARMMethodPtrABI),
42 use32BitVTableOffsetABI(use32BitVTableOffsetABI) {}
47 lowerDataMemberType(cir::DataMemberType type,
48 const mlir::TypeConverter &typeConverter)
const override;
51 lowerMethodType(cir::MethodType type,
52 const mlir::TypeConverter &typeConverter)
const override;
54 mlir::TypedAttr lowerDataMemberConstant(
55 cir::DataMemberAttr attr,
const mlir::DataLayout &layout,
56 const mlir::TypeConverter &typeConverter)
const override;
58 mlir::TypedAttr lowerDataMemberOffsetConstant(
59 cir::DataMemberOffsetAttr attr,
const mlir::DataLayout &layout,
60 const mlir::TypeConverter &typeConverter)
const override;
63 lowerMethodConstant(cir::MethodAttr attr,
const mlir::DataLayout &layout,
64 const mlir::TypeConverter &typeConverter)
const override;
67 lowerGetRuntimeMember(cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
68 mlir::Value loweredAddr, mlir::Value loweredMember,
69 mlir::OpBuilder &builder)
const override;
71 void lowerGetMethod(cir::GetMethodOp op, mlir::Value &callee,
72 mlir::Value &thisArg, mlir::Value loweredMethod,
73 mlir::Value loweredObjectPtr,
74 mlir::ConversionPatternRewriter &rewriter)
const override;
76 mlir::Value lowerBaseDataMember(cir::BaseDataMemberOp op,
77 mlir::Value loweredSrc,
78 mlir::OpBuilder &builder)
const override;
80 mlir::Value lowerDerivedDataMember(cir::DerivedDataMemberOp op,
81 mlir::Value loweredSrc,
82 mlir::OpBuilder &builder)
const override;
84 mlir::Value lowerBaseMethod(cir::BaseMethodOp op, mlir::Value loweredSrc,
85 mlir::OpBuilder &builder)
const override;
87 mlir::Value lowerDerivedMethod(cir::DerivedMethodOp op,
88 mlir::Value loweredSrc,
89 mlir::OpBuilder &builder)
const override;
91 mlir::Value lowerDataMemberCmp(cir::CmpOp op, mlir::Value loweredLhs,
92 mlir::Value loweredRhs,
93 mlir::OpBuilder &builder)
const override;
95 mlir::Value lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs,
96 mlir::Value loweredRhs,
97 mlir::OpBuilder &builder)
const override;
99 mlir::Value lowerDataMemberBitcast(cir::CastOp op, mlir::Type loweredDstTy,
100 mlir::Value loweredSrc,
101 mlir::OpBuilder &builder)
const override;
104 lowerDataMemberToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
105 mlir::OpBuilder &builder)
const override;
107 mlir::Value lowerMethodBitcast(cir::CastOp op, mlir::Type loweredDstTy,
108 mlir::Value loweredSrc,
109 mlir::OpBuilder &builder)
const override;
111 mlir::Value lowerMethodToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
112 mlir::OpBuilder &builder)
const override;
114 mlir::Value lowerDynamicCast(cir::DynamicCastOp op,
115 mlir::OpBuilder &builder)
const override;
116 mlir::Value lowerVTableGetTypeInfo(cir::VTableGetTypeInfoOp op,
117 mlir::OpBuilder &builder)
const override;
120 getArrayCookieSizeImpl(clang::CharUnits elementAlign)
const override;
122 mlir::Value readArrayCookieImpl(mlir::Location loc, mlir::Value allocPtr,
123 clang::CharUnits cookieSize,
124 clang::CharUnits cookieAlignment,
125 const mlir::DataLayout &dataLayout,
126 CIRBaseBuilderTy &builder)
const override;
136 case clang::TargetCXXABI::GenericAArch64:
137 return std::make_unique<LowerItaniumCXXABI>(
141 case clang::TargetCXXABI::AppleARM64:
145 return std::make_unique<LowerItaniumCXXABI>(
150 case clang::TargetCXXABI::GenericItanium:
151 return std::make_unique<LowerItaniumCXXABI>(lm);
153 case clang::TargetCXXABI::GenericARM:
154 case clang::TargetCXXABI::iOS:
155 case clang::TargetCXXABI::WatchOS:
156 case clang::TargetCXXABI::Fuchsia:
157 case clang::TargetCXXABI::GenericMIPS:
158 case clang::TargetCXXABI::WebAssembly:
159 case clang::TargetCXXABI::XL:
164 case clang::TargetCXXABI::Microsoft:
165 llvm_unreachable(
"Microsoft ABI is not Itanium-based");
167 llvm_unreachable(
"bad ABI kind");
178mlir::Type LowerItaniumCXXABI::lowerDataMemberType(
179 cir::DataMemberType type,
const mlir::TypeConverter &typeConverter)
const {
183 return getPtrDiffCIRTy(lm);
186mlir::Type LowerItaniumCXXABI::lowerMethodType(
187 cir::MethodType type,
const mlir::TypeConverter &typeConverter)
const {
202 mlir::Type members[] = {ptrdiffCIRTy, ptrdiffCIRTy};
203 return cir::StructType::get(
type.getContext(), members,
false,
208mlir::TypedAttr LowerItaniumCXXABI::lowerDataMemberConstant(
209 cir::DataMemberAttr attr,
const mlir::DataLayout &layout,
210 const mlir::TypeConverter &typeConverter)
const {
212 if (
attr.isNullPtr()) {
222 mlir::Type currentTy =
attr.getType().getClassTy();
224 auto recTy = mlir::cast<cir::RecordType>(currentTy);
225 memberOffset +=
static_cast<int64_t>(recTy.getElementOffset(layout, idx));
226 currentTy = recTy.getMembers()[idx];
230 mlir::Type abiTy = lowerDataMemberType(
attr.getType(), typeConverter);
231 return cir::IntAttr::get(abiTy, memberOffset);
234mlir::TypedAttr LowerItaniumCXXABI::lowerDataMemberOffsetConstant(
235 cir::DataMemberOffsetAttr attr,
const mlir::DataLayout &layout,
236 const mlir::TypeConverter &typeConverter)
const {
241 mlir::Type abiTy = lowerDataMemberType(
attr.getType(), typeConverter);
242 return cir::IntAttr::get(abiTy,
static_cast<int64_t>(
attr.getOffset()));
245mlir::TypedAttr LowerItaniumCXXABI::lowerMethodConstant(
246 cir::MethodAttr attr,
const mlir::DataLayout &layout,
247 const mlir::TypeConverter &typeConverter)
const {
250 auto loweredMethodTy = mlir::cast<cir::StructType>(
251 lowerMethodType(
attr.getType(), typeConverter));
253 auto zero = cir::IntAttr::get(ptrdiffCIRTy, 0);
274 return cir::ConstRecordAttr::get(
275 loweredMethodTy, mlir::ArrayAttr::get(
attr.getContext(), {zero, zero}));
278 if (
attr.isVirtual()) {
284 if (useARMMethodPtrABI) {
292 auto ptr = cir::IntAttr::get(ptrdiffCIRTy, vtableOffset);
293 auto one = cir::IntAttr::get(ptrdiffCIRTy, 1);
294 return cir::ConstRecordAttr::get(
295 loweredMethodTy, mlir::ArrayAttr::get(
attr.getContext(), {ptr, one}));
305 auto ptr = cir::IntAttr::get(ptrdiffCIRTy, 1 + vtableOffset);
306 return cir::ConstRecordAttr::get(
307 loweredMethodTy, mlir::ArrayAttr::get(
attr.getContext(), {ptr, zero}));
315 auto ptr = cir::GlobalViewAttr::get(ptrdiffCIRTy,
attr.getSymbol().value());
316 return cir::ConstRecordAttr::get(
317 loweredMethodTy, mlir::ArrayAttr::get(
attr.getContext(), {ptr, zero}));
320mlir::Operation *LowerItaniumCXXABI::lowerGetRuntimeMember(
321 cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
322 mlir::Value loweredAddr, mlir::Value loweredMember,
323 mlir::OpBuilder &builder)
const {
324 auto byteTy = cir::IntType::get(op.getContext(), 8,
true);
325 auto bytePtrTy = cir::PointerType::get(
327 mlir::cast<cir::PointerType>(op.getAddr().getType()).getAddrSpace());
328 auto objectBytesPtr = cir::CastOp::create(
329 builder, op.getLoc(), bytePtrTy, cir::CastKind::bitcast, op.getAddr());
330 auto memberBytesPtr = cir::PtrStrideOp::create(
331 builder, op.getLoc(), bytePtrTy, objectBytesPtr, loweredMember);
332 return cir::CastOp::create(builder, op.getLoc(), op.getType(),
333 cir::CastKind::bitcast, memberBytesPtr);
336void LowerItaniumCXXABI::lowerGetMethod(
337 cir::GetMethodOp op, mlir::Value &callee, mlir::Value &thisArg,
338 mlir::Value loweredMethod, mlir::Value loweredObjectPtr,
339 mlir::ConversionPatternRewriter &rewriter)
const {
361 mlir::ImplicitLocOpBuilder locBuilder(op.getLoc(), rewriter);
362 mlir::Type calleePtrTy = op.getCallee().getType();
365 mlir::Value ptrdiffOne =
366 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 1));
369 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredMethod, 1);
370 mlir::Value adj = rawAdj;
371 if (useARMMethodPtrABI)
372 adj = cir::ShiftOp::create(locBuilder, ptrdiffCIRTy, adj, ptrdiffOne,
376 mlir::Type thisVoidPtrTy =
377 cir::PointerType::get(cir::VoidType::get(locBuilder.getContext()),
378 op.getObject().getType().getAddrSpace());
379 mlir::Value thisVoidPtr = cir::CastOp::create(
380 locBuilder, thisVoidPtrTy, cir::CastKind::bitcast, loweredObjectPtr);
382 cir::PtrStrideOp::create(locBuilder, thisVoidPtrTy, thisVoidPtr, adj);
386 mlir::Value methodPtrField =
387 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredMethod, 0);
388 mlir::Value virtualBit;
389 if (useARMMethodPtrABI)
390 virtualBit = cir::AndOp::create(locBuilder, rawAdj, ptrdiffOne);
392 virtualBit = cir::AndOp::create(locBuilder, methodPtrField, ptrdiffOne);
393 mlir::Value isVirtual = cir::CmpOp::create(locBuilder, cir::CmpOpKind::eq,
394 virtualBit, ptrdiffOne);
400 auto buildVirtualCallee = [&](mlir::OpBuilder &b, mlir::Location loc) {
402 auto vtablePtrPtr = cir::VTableGetVPtrOp::create(b, loc, thisArg);
404 mlir::Value vtablePtr =
405 cir::LoadOp::create(b, loc, vtablePtrPtr,
false,
409 cir::SyncScopeKindAttr{},
416 mlir::Value vtableOffset = methodPtrField;
417 if (!useARMMethodPtrABI)
418 vtableOffset = cir::SubOp::create(b, loc, vtableOffset.getType(),
419 vtableOffset, ptrdiffOne);
420 if (use32BitVTableOffsetABI)
421 llvm_unreachable(
"AppleARM64 method ptr abi NYI");
429 mlir::Value fnPtr = cir::VTableGetVirtualFnByOffsetOp::create(
430 b, loc, calleePtrTy, vtablePtr, vtableOffset);
432 cir::YieldOp::create(b, loc, fnPtr);
436 callee = cir::TernaryOp::create(
437 locBuilder, isVirtual, buildVirtualCallee,
439 [&](mlir::OpBuilder &b, mlir::Location loc) {
440 auto fnPtr = cir::CastOp::create(b, loc, calleePtrTy,
441 cir::CastKind::int_to_ptr,
443 cir::YieldOp::create(b, loc, fnPtr.getResult());
449 mlir::Value loweredSrc,
451 bool isDerivedToBase,
452 mlir::OpBuilder &builder) {
455 mlir::Location loc = op->getLoc();
456 mlir::Type ty = loweredSrc.getType();
458 auto getConstantInt = [&](
int64_t value) -> cir::ConstantOp {
459 return cir::ConstantOp::create(builder, loc, cir::IntAttr::get(ty, value));
462 cir::ConstantOp nullValue = getConstantInt(-1);
463 auto isNull = cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, loweredSrc,
466 cir::ConstantOp offsetValue = getConstantInt(offset);
467 mlir::Value adjustedPtr;
469 adjustedPtr = cir::SubOp::create(builder, loc, loweredSrc, offsetValue,
470 cir::OverflowFlags::NoSignedWrap);
472 adjustedPtr = cir::AddOp::create(builder, loc, loweredSrc, offsetValue,
473 cir::OverflowFlags::NoSignedWrap);
475 return cir::SelectOp::create(builder, loc, ty, isNull, loweredSrc,
480LowerItaniumCXXABI::lowerBaseDataMember(cir::BaseDataMemberOp op,
481 mlir::Value loweredSrc,
482 mlir::OpBuilder &builder)
const {
483 return lowerDataMemberCast(op, loweredSrc, op.getOffset().getSExtValue(),
488LowerItaniumCXXABI::lowerDerivedDataMember(cir::DerivedDataMemberOp op,
489 mlir::Value loweredSrc,
490 mlir::OpBuilder &builder)
const {
496 std::int64_t offset,
bool isDerivedToBase,
497 bool useARMMethodPtrABI,
499 mlir::OpBuilder &builder) {
505 if (useARMMethodPtrABI)
509 auto adjField = cir::ExtractMemberOp::create(builder, op->getLoc(),
510 ptrdiffCIRTy, loweredSrc, 1);
512 auto offsetValue = cir::ConstantOp::create(
513 builder, op->getLoc(), cir::IntAttr::get(ptrdiffCIRTy, offset));
514 mlir::Value adjustedAdjField;
517 cir::SubOp::create(builder, op->getLoc(), adjField, offsetValue,
518 cir::OverflowFlags::NoSignedWrap);
521 cir::AddOp::create(builder, op->getLoc(), adjField, offsetValue,
522 cir::OverflowFlags::NoSignedWrap);
524 return cir::InsertMemberOp::create(builder, op->getLoc(), loweredSrc, 1,
529LowerItaniumCXXABI::lowerBaseMethod(cir::BaseMethodOp op,
530 mlir::Value loweredSrc,
531 mlir::OpBuilder &builder)
const {
532 return lowerMethodCast(op, loweredSrc, op.getOffset().getSExtValue(),
533 true, useARMMethodPtrABI, lm,
538LowerItaniumCXXABI::lowerDerivedMethod(cir::DerivedMethodOp op,
539 mlir::Value loweredSrc,
540 mlir::OpBuilder &builder)
const {
542 false, useARMMethodPtrABI, lm,
547LowerItaniumCXXABI::lowerDataMemberCmp(cir::CmpOp op, mlir::Value loweredLhs,
548 mlir::Value loweredRhs,
549 mlir::OpBuilder &builder)
const {
550 return cir::CmpOp::create(builder, op.getLoc(), op.getKind(), loweredLhs,
554mlir::Value LowerItaniumCXXABI::lowerMethodCmp(cir::CmpOp op,
555 mlir::Value loweredLhs,
556 mlir::Value loweredRhs,
557 mlir::OpBuilder &builder)
const {
558 assert(op.getKind() == cir::CmpOpKind::eq ||
559 op.getKind() == cir::CmpOpKind::ne);
561 mlir::ImplicitLocOpBuilder locBuilder(op.getLoc(), builder);
563 mlir::Value ptrdiffZero =
564 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 0));
566 mlir::Value lhsPtrField =
567 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredLhs, 0);
568 mlir::Value rhsPtrField =
569 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredRhs, 0);
571 cir::CmpOp::create(locBuilder, op.getKind(), lhsPtrField, rhsPtrField);
572 mlir::Value ptrCmpToNull =
573 cir::CmpOp::create(locBuilder, op.getKind(), lhsPtrField, ptrdiffZero);
575 mlir::Value lhsAdjField =
576 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredLhs, 1);
577 mlir::Value rhsAdjField =
578 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredRhs, 1);
580 cir::CmpOp::create(locBuilder, op.getKind(), lhsAdjField, rhsAdjField);
582 auto create_and = [&](mlir::Value lhs, mlir::Value rhs) {
583 return cir::AndOp::create(locBuilder, lhs.getType(), lhs, rhs);
585 auto create_or = [&](mlir::Value lhs, mlir::Value rhs) {
586 return cir::OrOp::create(locBuilder, lhs.getType(), lhs, rhs);
591 if (useARMMethodPtrABI) {
593 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 1));
609 mlir::Value orAdj = create_or(lhsAdjField, rhsAdjField);
610 mlir::Value orAdjAnd1 = create_and(orAdj, one);
611 mlir::Value orAdjAnd1CmpZero =
612 cir::CmpOp::create(locBuilder, op.getKind(), orAdjAnd1, ptrdiffZero);
614 if (op.getKind() == cir::CmpOpKind::eq)
615 ptrCmpToNull = create_and(ptrCmpToNull, orAdjAnd1CmpZero);
617 ptrCmpToNull = create_or(ptrCmpToNull, orAdjAnd1CmpZero);
621 if (op.getKind() == cir::CmpOpKind::eq) {
623 result = create_and(ptrCmp, create_or(ptrCmpToNull, adjCmp));
626 result = create_or(ptrCmp, create_and(ptrCmpToNull, adjCmp));
632mlir::Value LowerItaniumCXXABI::lowerDataMemberBitcast(
633 cir::CastOp op, mlir::Type loweredDstTy, mlir::Value loweredSrc,
634 mlir::OpBuilder &builder)
const {
635 if (loweredSrc.getType() == loweredDstTy)
638 return cir::CastOp::create(builder, op.getLoc(), loweredDstTy,
639 cir::CastKind::bitcast, loweredSrc);
642mlir::Value LowerItaniumCXXABI::lowerDataMemberToBoolCast(
643 cir::CastOp op, mlir::Value loweredSrc, mlir::OpBuilder &builder)
const {
647 auto nullValue = cir::ConstantOp::create(builder, op.getLoc(), nullAttr);
648 return cir::CmpOp::create(builder, op.getLoc(), cir::CmpOpKind::ne,
649 loweredSrc, nullValue);
653LowerItaniumCXXABI::lowerMethodBitcast(cir::CastOp op, mlir::Type loweredDstTy,
654 mlir::Value loweredSrc,
655 mlir::OpBuilder &builder)
const {
656 if (loweredSrc.getType() == loweredDstTy)
662mlir::Value LowerItaniumCXXABI::lowerMethodToBoolCast(
663 cir::CastOp op, mlir::Value loweredSrc, mlir::OpBuilder &builder)
const {
664 mlir::ImplicitLocOpBuilder locBuilder(op.getLoc(), builder);
672 mlir::Value ptrdiffZero =
673 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 0));
674 mlir::Value ptrField =
675 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredSrc, 0);
678 cir::CmpOp::create(locBuilder, cir::CmpOpKind::ne, ptrField, ptrdiffZero);
682 if (useARMMethodPtrABI) {
684 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 1));
686 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredSrc, 1);
687 mlir::Value virtualBit =
688 cir::AndOp::create(locBuilder, ptrdiffCIRTy, adj, one);
689 mlir::Value isVirtual = cir::CmpOp::create(locBuilder, cir::CmpOpKind::ne,
690 virtualBit, ptrdiffZero);
691 result = cir::OrOp::create(locBuilder, result, isVirtual);
698 mlir::FlatSymbolRefAttr badCastFuncRef) {
701 auto callOp = cir::CallOp::create(builder, loc, badCastFuncRef,
704 callOp->setAttr(cir::CIRDialect::getNoReturnAttrName(),
705 builder.getUnitAttr());
707 cir::UnreachableOp::create(builder, loc);
708 builder.clearInsertionPoint();
712 mlir::OpBuilder &builder) {
713 mlir::Location loc = op->getLoc();
714 mlir::Value srcValue = op.getSrc();
715 cir::DynamicCastInfoAttr castInfo = op.getInfo().value();
721 cir::PointerType::get(cir::VoidType::get(builder.getContext()));
723 mlir::Value srcPtr = cir::CastOp::create(builder, loc, voidPtrTy,
724 cir::CastKind::bitcast, srcValue);
725 mlir::Value srcRtti =
726 cir::ConstantOp::create(builder, loc, castInfo.getSrcRtti());
727 mlir::Value destRtti =
728 cir::ConstantOp::create(builder, loc, castInfo.getDestRtti());
729 mlir::Value offsetHint =
730 cir::ConstantOp::create(builder, loc, castInfo.getOffsetHint());
732 mlir::FlatSymbolRefAttr dynCastFuncRef = castInfo.getRuntimeFunc();
733 mlir::Value dynCastFuncArgs[4] = {srcPtr, srcRtti, destRtti, offsetHint};
738 mlir::Value castedPtr = cir::CallOp::create(builder, loc, dynCastFuncRef,
739 voidPtrTy, dynCastFuncArgs)
742 assert(mlir::isa<cir::PointerType>(castedPtr.getType()) &&
743 "the return value of __dynamic_cast should be a ptr");
747 if (op.isRefCast()) {
749 mlir::Value null = cir::ConstantOp::create(
751 cir::ConstPtrAttr::get(castedPtr.getType(),
752 builder.getI64IntegerAttr(0)));
753 mlir::Value castedPtrIsNull =
754 cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, castedPtr, null);
755 cir::IfOp::create(builder, loc, castedPtrIsNull,
false,
756 [&](mlir::OpBuilder &, mlir::Location) {
758 castInfo.getBadCastFunc());
764 return cir::CastOp::create(builder, loc, op.getType(), cir::CastKind::bitcast,
770 mlir::Location loc = op.getLoc();
777 uint64_t vptrAlign = llvm::divideCeil(
779 auto vptrPtr = cir::VTableGetVPtrOp::create(builder, loc, op.getSrc());
781 cir::LoadOp::create(builder, loc, vptrPtr,
785 builder.getI64IntegerAttr(vptrAlign),
786 cir::SyncScopeKindAttr(),
791 mlir::Value offsetToTop = cir::VTableGetOffsetOp::create(
797 cir::PointerType::get(cir::IntType::get(builder.getContext(), 8,
799 op.getSrc().getType().getAddrSpace());
800 mlir::Value srcBytePtr = cir::CastOp::create(
801 builder, loc, u8PtrTy, cir::CastKind::bitcast, op.getSrc());
803 cir::PtrStrideOp::create(builder, loc, u8PtrTy, srcBytePtr, offsetToTop);
805 return cir::CastOp::create(builder, loc, op.getType(), cir::CastKind::bitcast,
810LowerItaniumCXXABI::lowerDynamicCast(cir::DynamicCastOp op,
811 mlir::OpBuilder &builder)
const {
812 mlir::Location loc = op->getLoc();
813 mlir::Value srcValue = op.getSrc();
818 return buildDynamicCastAfterNullCheck(op, builder);
820 mlir::Value srcValueIsNotNull = cir::CastOp::create(
821 builder, loc, cir::BoolType::get(builder.getContext()),
822 cir::CastKind::ptr_to_bool, srcValue);
823 return cir::TernaryOp::create(
824 builder, loc, srcValueIsNotNull,
825 [&](mlir::OpBuilder &, mlir::Location) {
826 mlir::Value castedValue =
828 ? buildDynamicCastToVoidAfterNullCheck(op, lm, builder)
829 : buildDynamicCastAfterNullCheck(op, builder);
830 cir::YieldOp::create(builder, loc, castedValue);
832 [&](mlir::OpBuilder &, mlir::Location) {
833 mlir::Value null = cir::ConstantOp::create(
835 cir::ConstPtrAttr::get(op.getType(),
836 builder.getI64IntegerAttr(0)));
837 cir::YieldOp::create(builder, loc, null);
842LowerItaniumCXXABI::lowerVTableGetTypeInfo(cir::VTableGetTypeInfoOp op,
843 mlir::OpBuilder &builder)
const {
846 return cir::VTableGetEntryOp::create(builder, op->getLoc(), op.getType(),
850clang::CharUnits LowerItaniumCXXABI::getArrayCookieSizeImpl(
851 clang::CharUnits elementAlign)
const {
854 clang::CharUnits sizeOfSizeT =
856 return std::max(sizeOfSizeT, elementAlign);
859mlir::Value LowerItaniumCXXABI::readArrayCookieImpl(
860 mlir::Location loc, mlir::Value allocPtr, clang::CharUnits cookieSize,
861 clang::CharUnits cookieAlignment,
const mlir::DataLayout &dataLayout,
862 CIRBaseBuilderTy &builder)
const {
863 unsigned ptrSizeInBits = getPtrSizeInBits();
864 mlir::ptr::MemorySpaceAttrInterface addrSpace =
865 mlir::cast<cir::PointerType>(allocPtr.getType()).getAddrSpace();
867 auto ptrDiffTy = builder.
getSIntNTy(ptrSizeInBits);
868 auto sizeTy = builder.
getUIntNTy(ptrSizeInBits);
871 clang::CharUnits sizeOfSizeT =
873 clang::CharUnits countOffset = cookieSize - sizeOfSizeT;
875 mlir::Value countBytePtr = allocPtr;
876 clang::CharUnits countAlignment = cookieAlignment;
877 if (!countOffset.
isZero()) {
878 mlir::Value offsetVal = cir::ConstantOp::create(
879 builder, loc, cir::IntAttr::get(ptrDiffTy, countOffset.
getQuantity()));
881 cir::PtrStrideOp::create(builder, loc, u8PtrTy, allocPtr, offsetVal);
885 auto countPtrTy = cir::PointerType::get(sizeTy, addrSpace);
886 mlir::Value countPtr = cir::CastOp::create(
887 builder, loc, countPtrTy, cir::CastKind::bitcast, countBytePtr);
888 return cir::LoadOp::create(
889 builder, loc, countPtr,
false,
false,
891 builder.getI64IntegerAttr(countAlignment.
getQuantity()),
892 cir::SyncScopeKindAttr(), cir::MemOrderAttr(),
cir::PointerType getPointerTo(mlir::Type ty)
cir::IntType getUIntNTy(int n)
cir::IntType getSIntNTy(int n)
mlir::MLIRContext * getMLIRContext()
clang::TargetCXXABI::Kind getCXXABIKind() const
const clang::TargetInfo & getTarget() const
static llvm::SmallVector< RecordMemberKind > getAllDataKinds(llvm::ArrayRef< mlir::Type > members)
One Data kind per member.
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset?
bool isZero() const
Test whether the quantity equals zero.
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
static constexpr CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
Exposes information about the current target.
unsigned getTypeWidth(IntType T) const
Return the width (in bits) of the specified integer type enum.
static bool isTypeSigned(IntType T)
Returns true if the type is signed; false otherwise.
IntType getPtrDiffType(LangAS AddrSpace) const
uint64_t getPointerAlign(LangAS AddrSpace) const
static mlir::Value lowerDataMemberCast(mlir::Operation *op, mlir::Value loweredSrc, std::int64_t offset, bool isDerivedToBase, mlir::OpBuilder &builder)
std::unique_ptr< CIRCXXABI > createItaniumCXXABI(LowerModule &lm)
Creates an Itanium-family ABI.
static mlir::Value buildDynamicCastAfterNullCheck(cir::DynamicCastOp op, mlir::OpBuilder &builder)
uint64_t getVTableEntrySize(mlir::ModuleOp mod, const mlir::DataLayout &dataLayout)
The size in bytes of an entry of a C++ vtable in module.
static cir::IntType getPtrDiffCIRTy(LowerModule &lm)
static mlir::Value buildDynamicCastToVoidAfterNullCheck(cir::DynamicCastOp op, cir::LowerModule &lm, mlir::OpBuilder &builder)
static mlir::Value lowerMethodCast(mlir::Operation *op, mlir::Value loweredSrc, std::int64_t offset, bool isDerivedToBase, bool useARMMethodPtrABI, LowerModule &lowerMod, mlir::OpBuilder &builder)
static void buildBadCastCall(mlir::OpBuilder &builder, mlir::Location loc, mlir::FlatSymbolRefAttr badCastFuncRef)
const internal::VariadicAllOfMatcher< Attr > attr
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t
static bool addressSpace()
static bool appleArm64CXXABI()
static bool emitCFICheck()
static bool emitVFEInfo()
static bool emitWPDInfo()
static bool emitTypeCheck()
static bool pointerAuthentication()
static bool opFuncCallingConv()
IntType
===-— Target Data Type Query Methods ----------------------------—===//