25#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
26#include "mlir/IR/ImplicitLocOpBuilder.h"
28#include "llvm/Support/ErrorHandling.h"
34class LowerItaniumCXXABI :
public CIRCXXABI {
36 bool useARMMethodPtrABI;
37 bool use32BitVTableOffsetABI;
40 LowerItaniumCXXABI(LowerModule &lm,
bool useARMMethodPtrABI =
false,
41 bool use32BitVTableOffsetABI =
false)
42 : CIRCXXABI(lm), useARMMethodPtrABI(useARMMethodPtrABI),
43 use32BitVTableOffsetABI(use32BitVTableOffsetABI) {}
48 lowerDataMemberType(cir::DataMemberType type,
49 const mlir::TypeConverter &typeConverter)
const override;
52 lowerMethodType(cir::MethodType type,
53 const mlir::TypeConverter &typeConverter)
const override;
55 mlir::TypedAttr lowerDataMemberConstant(
56 cir::DataMemberAttr attr,
const mlir::DataLayout &layout,
57 const mlir::TypeConverter &typeConverter)
const override;
59 mlir::TypedAttr lowerDataMemberOffsetConstant(
60 cir::DataMemberOffsetAttr attr,
const mlir::DataLayout &layout,
61 const mlir::TypeConverter &typeConverter)
const override;
64 lowerMethodConstant(cir::MethodAttr attr,
const mlir::DataLayout &layout,
65 const mlir::TypeConverter &typeConverter)
const override;
68 lowerGetRuntimeMember(cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
69 mlir::Value loweredAddr, mlir::Value loweredMember,
70 mlir::OpBuilder &builder)
const override;
72 void lowerGetMethod(cir::GetMethodOp op, mlir::Value &callee,
73 mlir::Value &thisArg, mlir::Value loweredMethod,
74 mlir::Value loweredObjectPtr,
75 mlir::ConversionPatternRewriter &rewriter)
const override;
77 mlir::Value lowerBaseDataMember(cir::BaseDataMemberOp op,
78 mlir::Value loweredSrc,
79 mlir::OpBuilder &builder)
const override;
81 mlir::Value lowerDerivedDataMember(cir::DerivedDataMemberOp op,
82 mlir::Value loweredSrc,
83 mlir::OpBuilder &builder)
const override;
85 mlir::Value lowerBaseMethod(cir::BaseMethodOp op, mlir::Value loweredSrc,
86 mlir::OpBuilder &builder)
const override;
88 mlir::Value lowerDerivedMethod(cir::DerivedMethodOp op,
89 mlir::Value loweredSrc,
90 mlir::OpBuilder &builder)
const override;
92 mlir::Value lowerDataMemberCmp(cir::CmpOp op, mlir::Value loweredLhs,
93 mlir::Value loweredRhs,
94 mlir::OpBuilder &builder)
const override;
96 mlir::Value lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs,
97 mlir::Value loweredRhs,
98 mlir::OpBuilder &builder)
const override;
100 mlir::Value lowerDataMemberBitcast(cir::CastOp op, mlir::Type loweredDstTy,
101 mlir::Value loweredSrc,
102 mlir::OpBuilder &builder)
const override;
105 lowerDataMemberToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
106 mlir::OpBuilder &builder)
const override;
108 mlir::Value lowerMethodBitcast(cir::CastOp op, mlir::Type loweredDstTy,
109 mlir::Value loweredSrc,
110 mlir::OpBuilder &builder)
const override;
112 mlir::Value lowerMethodToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
113 mlir::OpBuilder &builder)
const override;
115 mlir::Value lowerDynamicCast(cir::DynamicCastOp op,
116 mlir::OpBuilder &builder)
const override;
117 mlir::Value lowerVTableGetTypeInfo(cir::VTableGetTypeInfoOp op,
118 mlir::OpBuilder &builder)
const override;
121 getArrayCookieSizeImpl(clang::CharUnits elementAlign)
const override;
123 mlir::Value readArrayCookieImpl(mlir::Location loc, mlir::Value allocPtr,
124 clang::CharUnits cookieSize,
125 clang::CharUnits cookieAlignment,
126 const mlir::DataLayout &dataLayout,
127 CIRBaseBuilderTy &builder)
const override;
137 case clang::TargetCXXABI::GenericAArch64:
138 return std::make_unique<LowerItaniumCXXABI>(
142 case clang::TargetCXXABI::AppleARM64:
146 return std::make_unique<LowerItaniumCXXABI>(
151 case clang::TargetCXXABI::GenericItanium:
152 return std::make_unique<LowerItaniumCXXABI>(lm);
154 case clang::TargetCXXABI::GenericARM:
155 case clang::TargetCXXABI::iOS:
156 case clang::TargetCXXABI::WatchOS:
157 case clang::TargetCXXABI::Fuchsia:
158 case clang::TargetCXXABI::GenericMIPS:
159 case clang::TargetCXXABI::WebAssembly:
160 case clang::TargetCXXABI::XL:
165 case clang::TargetCXXABI::Microsoft:
166 llvm_unreachable(
"Microsoft ABI is not Itanium-based");
168 llvm_unreachable(
"bad ABI kind");
179mlir::Type LowerItaniumCXXABI::lowerDataMemberType(
180 cir::DataMemberType type,
const mlir::TypeConverter &typeConverter)
const {
184 return getPtrDiffCIRTy(lm);
187mlir::Type LowerItaniumCXXABI::lowerMethodType(
188 cir::MethodType type,
const mlir::TypeConverter &typeConverter)
const {
203 mlir::Type members[] = {ptrdiffCIRTy, ptrdiffCIRTy};
204 return cir::StructType::get(
type.getContext(), members,
false,
209mlir::TypedAttr LowerItaniumCXXABI::lowerDataMemberConstant(
210 cir::DataMemberAttr attr,
const mlir::DataLayout &layout,
211 const mlir::TypeConverter &typeConverter)
const {
213 if (
attr.isNullPtr()) {
223 mlir::Type currentTy =
attr.getType().getClassTy();
225 auto recTy = mlir::cast<cir::RecordType>(currentTy);
226 memberOffset +=
static_cast<int64_t>(recTy.getElementOffset(layout, idx));
227 currentTy = recTy.getMembers()[idx];
231 mlir::Type abiTy = lowerDataMemberType(
attr.getType(), typeConverter);
232 return cir::IntAttr::get(abiTy, memberOffset);
235mlir::TypedAttr LowerItaniumCXXABI::lowerDataMemberOffsetConstant(
236 cir::DataMemberOffsetAttr attr,
const mlir::DataLayout &layout,
237 const mlir::TypeConverter &typeConverter)
const {
242 mlir::Type abiTy = lowerDataMemberType(
attr.getType(), typeConverter);
243 return cir::IntAttr::get(abiTy,
static_cast<int64_t>(
attr.getOffset()));
246mlir::TypedAttr LowerItaniumCXXABI::lowerMethodConstant(
247 cir::MethodAttr attr,
const mlir::DataLayout &layout,
248 const mlir::TypeConverter &typeConverter)
const {
251 auto loweredMethodTy = mlir::cast<cir::StructType>(
252 lowerMethodType(
attr.getType(), typeConverter));
254 auto zero = cir::IntAttr::get(ptrdiffCIRTy, 0);
275 return cir::ConstRecordAttr::get(
276 loweredMethodTy, mlir::ArrayAttr::get(
attr.getContext(), {zero, zero}));
279 if (
attr.isVirtual()) {
280 if (useARMMethodPtrABI) {
289 cir::IntAttr::get(ptrdiffCIRTy,
attr.getVtableOffset().value());
290 auto one = cir::IntAttr::get(ptrdiffCIRTy, 1);
291 return cir::ConstRecordAttr::get(
292 loweredMethodTy, mlir::ArrayAttr::get(
attr.getContext(), {ptr, one}));
303 cir::IntAttr::get(ptrdiffCIRTy, 1 +
attr.getVtableOffset().value());
304 return cir::ConstRecordAttr::get(
305 loweredMethodTy, mlir::ArrayAttr::get(
attr.getContext(), {ptr, zero}));
313 auto ptr = cir::GlobalViewAttr::get(ptrdiffCIRTy,
attr.getSymbol().value());
314 return cir::ConstRecordAttr::get(
315 loweredMethodTy, mlir::ArrayAttr::get(
attr.getContext(), {ptr, zero}));
318mlir::Operation *LowerItaniumCXXABI::lowerGetRuntimeMember(
319 cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
320 mlir::Value loweredAddr, mlir::Value loweredMember,
321 mlir::OpBuilder &builder)
const {
322 auto byteTy = cir::IntType::get(op.getContext(), 8,
true);
323 auto bytePtrTy = cir::PointerType::get(
325 mlir::cast<cir::PointerType>(op.getAddr().getType()).getAddrSpace());
326 auto objectBytesPtr = cir::CastOp::create(
327 builder, op.getLoc(), bytePtrTy, cir::CastKind::bitcast, op.getAddr());
328 auto memberBytesPtr = cir::PtrStrideOp::create(
329 builder, op.getLoc(), bytePtrTy, objectBytesPtr, loweredMember);
330 return cir::CastOp::create(builder, op.getLoc(), op.getType(),
331 cir::CastKind::bitcast, memberBytesPtr);
334void LowerItaniumCXXABI::lowerGetMethod(
335 cir::GetMethodOp op, mlir::Value &callee, mlir::Value &thisArg,
336 mlir::Value loweredMethod, mlir::Value loweredObjectPtr,
337 mlir::ConversionPatternRewriter &rewriter)
const {
359 mlir::ImplicitLocOpBuilder locBuilder(op.getLoc(), rewriter);
360 mlir::Type calleePtrTy = op.getCallee().getType();
363 mlir::Value ptrdiffOne =
364 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 1));
367 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredMethod, 1);
368 mlir::Value adj = rawAdj;
369 if (useARMMethodPtrABI)
370 adj = cir::ShiftOp::create(locBuilder, ptrdiffCIRTy, adj, ptrdiffOne,
374 mlir::Type thisVoidPtrTy =
375 cir::PointerType::get(cir::VoidType::get(locBuilder.getContext()),
376 op.getObject().getType().getAddrSpace());
377 mlir::Value thisVoidPtr = cir::CastOp::create(
378 locBuilder, thisVoidPtrTy, cir::CastKind::bitcast, loweredObjectPtr);
380 cir::PtrStrideOp::create(locBuilder, thisVoidPtrTy, thisVoidPtr, adj);
384 mlir::Value methodPtrField =
385 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredMethod, 0);
386 mlir::Value virtualBit;
387 if (useARMMethodPtrABI)
388 virtualBit = cir::AndOp::create(locBuilder, rawAdj, ptrdiffOne);
390 virtualBit = cir::AndOp::create(locBuilder, methodPtrField, ptrdiffOne);
391 mlir::Value isVirtual = cir::CmpOp::create(locBuilder, cir::CmpOpKind::eq,
392 virtualBit, ptrdiffOne);
398 auto buildVirtualCallee = [&](mlir::OpBuilder &b, mlir::Location loc) {
402 cir::PointerType::get(cir::IntType::get(b.getContext(), 8,
true));
403 auto vtablePtrPtrTy = cir::PointerType::get(
404 vtablePtrTy, op.getObject().getType().getAddrSpace());
405 auto vtablePtrPtr = cir::CastOp::create(b, loc, vtablePtrPtrTy,
406 cir::CastKind::bitcast, thisArg);
408 mlir::Value vtablePtr =
409 cir::LoadOp::create(b, loc, vtablePtrPtr,
false,
413 cir::SyncScopeKindAttr{},
420 mlir::Value vtableOffset = methodPtrField;
421 if (!useARMMethodPtrABI)
422 vtableOffset = cir::SubOp::create(b, loc, vtableOffset.getType(),
423 vtableOffset, ptrdiffOne);
424 if (use32BitVTableOffsetABI)
425 llvm_unreachable(
"AppleARM64 method ptr abi NYI");
433 mlir::Value vfpAddr = cir::PtrStrideOp::create(locBuilder, vtablePtrTy,
434 vtablePtr, vtableOffset);
435 auto vfpPtrTy = cir::PointerType::get(calleePtrTy);
436 mlir::Value vfpPtr = cir::CastOp::create(locBuilder, vfpPtrTy,
437 cir::CastKind::bitcast, vfpAddr);
438 auto fnPtr = cir::LoadOp::create(b, loc, vfpPtr,
442 cir::SyncScopeKindAttr{},
446 cir::YieldOp::create(b, loc, fnPtr.getResult());
450 callee = cir::TernaryOp::create(
451 locBuilder, isVirtual, buildVirtualCallee,
453 [&](mlir::OpBuilder &b, mlir::Location loc) {
454 auto fnPtr = cir::CastOp::create(b, loc, calleePtrTy,
455 cir::CastKind::int_to_ptr,
457 cir::YieldOp::create(b, loc, fnPtr.getResult());
463 mlir::Value loweredSrc,
465 bool isDerivedToBase,
466 mlir::OpBuilder &builder) {
469 mlir::Location loc = op->getLoc();
470 mlir::Type ty = loweredSrc.getType();
472 auto getConstantInt = [&](
int64_t value) -> cir::ConstantOp {
473 return cir::ConstantOp::create(builder, loc, cir::IntAttr::get(ty, value));
476 cir::ConstantOp nullValue = getConstantInt(-1);
477 auto isNull = cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, loweredSrc,
480 cir::ConstantOp offsetValue = getConstantInt(offset);
481 mlir::Value adjustedPtr;
483 adjustedPtr = cir::SubOp::create(builder, loc, loweredSrc, offsetValue,
484 cir::OverflowFlags::NoSignedWrap);
486 adjustedPtr = cir::AddOp::create(builder, loc, loweredSrc, offsetValue,
487 cir::OverflowFlags::NoSignedWrap);
489 return cir::SelectOp::create(builder, loc, ty, isNull, loweredSrc,
494LowerItaniumCXXABI::lowerBaseDataMember(cir::BaseDataMemberOp op,
495 mlir::Value loweredSrc,
496 mlir::OpBuilder &builder)
const {
497 return lowerDataMemberCast(op, loweredSrc, op.getOffset().getSExtValue(),
502LowerItaniumCXXABI::lowerDerivedDataMember(cir::DerivedDataMemberOp op,
503 mlir::Value loweredSrc,
504 mlir::OpBuilder &builder)
const {
510 std::int64_t offset,
bool isDerivedToBase,
511 bool useARMMethodPtrABI,
513 mlir::OpBuilder &builder) {
519 if (useARMMethodPtrABI)
523 auto adjField = cir::ExtractMemberOp::create(builder, op->getLoc(),
524 ptrdiffCIRTy, loweredSrc, 1);
526 auto offsetValue = cir::ConstantOp::create(
527 builder, op->getLoc(), cir::IntAttr::get(ptrdiffCIRTy, offset));
528 mlir::Value adjustedAdjField;
531 cir::SubOp::create(builder, op->getLoc(), adjField, offsetValue,
532 cir::OverflowFlags::NoSignedWrap);
535 cir::AddOp::create(builder, op->getLoc(), adjField, offsetValue,
536 cir::OverflowFlags::NoSignedWrap);
538 return cir::InsertMemberOp::create(builder, op->getLoc(), loweredSrc, 1,
543LowerItaniumCXXABI::lowerBaseMethod(cir::BaseMethodOp op,
544 mlir::Value loweredSrc,
545 mlir::OpBuilder &builder)
const {
546 return lowerMethodCast(op, loweredSrc, op.getOffset().getSExtValue(),
547 true, useARMMethodPtrABI, lm,
552LowerItaniumCXXABI::lowerDerivedMethod(cir::DerivedMethodOp op,
553 mlir::Value loweredSrc,
554 mlir::OpBuilder &builder)
const {
556 false, useARMMethodPtrABI, lm,
561LowerItaniumCXXABI::lowerDataMemberCmp(cir::CmpOp op, mlir::Value loweredLhs,
562 mlir::Value loweredRhs,
563 mlir::OpBuilder &builder)
const {
564 return cir::CmpOp::create(builder, op.getLoc(), op.getKind(), loweredLhs,
568mlir::Value LowerItaniumCXXABI::lowerMethodCmp(cir::CmpOp op,
569 mlir::Value loweredLhs,
570 mlir::Value loweredRhs,
571 mlir::OpBuilder &builder)
const {
572 assert(op.getKind() == cir::CmpOpKind::eq ||
573 op.getKind() == cir::CmpOpKind::ne);
575 mlir::ImplicitLocOpBuilder locBuilder(op.getLoc(), builder);
577 mlir::Value ptrdiffZero =
578 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 0));
580 mlir::Value lhsPtrField =
581 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredLhs, 0);
582 mlir::Value rhsPtrField =
583 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredRhs, 0);
585 cir::CmpOp::create(locBuilder, op.getKind(), lhsPtrField, rhsPtrField);
586 mlir::Value ptrCmpToNull =
587 cir::CmpOp::create(locBuilder, op.getKind(), lhsPtrField, ptrdiffZero);
589 mlir::Value lhsAdjField =
590 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredLhs, 1);
591 mlir::Value rhsAdjField =
592 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredRhs, 1);
594 cir::CmpOp::create(locBuilder, op.getKind(), lhsAdjField, rhsAdjField);
596 auto create_and = [&](mlir::Value lhs, mlir::Value rhs) {
597 return cir::AndOp::create(locBuilder, lhs.getType(), lhs, rhs);
599 auto create_or = [&](mlir::Value lhs, mlir::Value rhs) {
600 return cir::OrOp::create(locBuilder, lhs.getType(), lhs, rhs);
605 if (useARMMethodPtrABI) {
607 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 1));
623 mlir::Value orAdj = create_or(lhsAdjField, rhsAdjField);
624 mlir::Value orAdjAnd1 = create_and(orAdj, one);
625 mlir::Value orAdjAnd1CmpZero =
626 cir::CmpOp::create(locBuilder, op.getKind(), orAdjAnd1, ptrdiffZero);
628 if (op.getKind() == cir::CmpOpKind::eq)
629 ptrCmpToNull = create_and(ptrCmpToNull, orAdjAnd1CmpZero);
631 ptrCmpToNull = create_or(ptrCmpToNull, orAdjAnd1CmpZero);
635 if (op.getKind() == cir::CmpOpKind::eq) {
637 result = create_and(ptrCmp, create_or(ptrCmpToNull, adjCmp));
640 result = create_or(ptrCmp, create_and(ptrCmpToNull, adjCmp));
646mlir::Value LowerItaniumCXXABI::lowerDataMemberBitcast(
647 cir::CastOp op, mlir::Type loweredDstTy, mlir::Value loweredSrc,
648 mlir::OpBuilder &builder)
const {
649 if (loweredSrc.getType() == loweredDstTy)
652 return cir::CastOp::create(builder, op.getLoc(), loweredDstTy,
653 cir::CastKind::bitcast, loweredSrc);
656mlir::Value LowerItaniumCXXABI::lowerDataMemberToBoolCast(
657 cir::CastOp op, mlir::Value loweredSrc, mlir::OpBuilder &builder)
const {
661 auto nullValue = cir::ConstantOp::create(builder, op.getLoc(), nullAttr);
662 return cir::CmpOp::create(builder, op.getLoc(), cir::CmpOpKind::ne,
663 loweredSrc, nullValue);
667LowerItaniumCXXABI::lowerMethodBitcast(cir::CastOp op, mlir::Type loweredDstTy,
668 mlir::Value loweredSrc,
669 mlir::OpBuilder &builder)
const {
670 if (loweredSrc.getType() == loweredDstTy)
676mlir::Value LowerItaniumCXXABI::lowerMethodToBoolCast(
677 cir::CastOp op, mlir::Value loweredSrc, mlir::OpBuilder &builder)
const {
678 mlir::ImplicitLocOpBuilder locBuilder(op.getLoc(), builder);
686 mlir::Value ptrdiffZero =
687 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 0));
688 mlir::Value ptrField =
689 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredSrc, 0);
692 cir::CmpOp::create(locBuilder, cir::CmpOpKind::ne, ptrField, ptrdiffZero);
696 if (useARMMethodPtrABI) {
698 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 1));
700 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredSrc, 1);
701 mlir::Value virtualBit =
702 cir::AndOp::create(locBuilder, ptrdiffCIRTy, adj, one);
703 mlir::Value isVirtual = cir::CmpOp::create(locBuilder, cir::CmpOpKind::ne,
704 virtualBit, ptrdiffZero);
705 result = cir::OrOp::create(locBuilder, result, isVirtual);
712 mlir::FlatSymbolRefAttr badCastFuncRef) {
715 auto callOp = cir::CallOp::create(builder, loc, badCastFuncRef,
718 callOp->setAttr(cir::CIRDialect::getNoReturnAttrName(),
719 builder.getUnitAttr());
721 cir::UnreachableOp::create(builder, loc);
722 builder.clearInsertionPoint();
726 mlir::OpBuilder &builder) {
727 mlir::Location loc = op->getLoc();
728 mlir::Value srcValue = op.getSrc();
729 cir::DynamicCastInfoAttr castInfo = op.getInfo().value();
735 cir::PointerType::get(cir::VoidType::get(builder.getContext()));
737 mlir::Value srcPtr = cir::CastOp::create(builder, loc, voidPtrTy,
738 cir::CastKind::bitcast, srcValue);
739 mlir::Value srcRtti =
740 cir::ConstantOp::create(builder, loc, castInfo.getSrcRtti());
741 mlir::Value destRtti =
742 cir::ConstantOp::create(builder, loc, castInfo.getDestRtti());
743 mlir::Value offsetHint =
744 cir::ConstantOp::create(builder, loc, castInfo.getOffsetHint());
746 mlir::FlatSymbolRefAttr dynCastFuncRef = castInfo.getRuntimeFunc();
747 mlir::Value dynCastFuncArgs[4] = {srcPtr, srcRtti, destRtti, offsetHint};
752 mlir::Value castedPtr = cir::CallOp::create(builder, loc, dynCastFuncRef,
753 voidPtrTy, dynCastFuncArgs)
756 assert(mlir::isa<cir::PointerType>(castedPtr.getType()) &&
757 "the return value of __dynamic_cast should be a ptr");
761 if (op.isRefCast()) {
763 mlir::Value null = cir::ConstantOp::create(
765 cir::ConstPtrAttr::get(castedPtr.getType(),
766 builder.getI64IntegerAttr(0)));
767 mlir::Value castedPtrIsNull =
768 cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, castedPtr, null);
769 cir::IfOp::create(builder, loc, castedPtrIsNull,
false,
770 [&](mlir::OpBuilder &, mlir::Location) {
772 castInfo.getBadCastFunc());
778 return cir::CastOp::create(builder, loc, op.getType(), cir::CastKind::bitcast,
784 mlir::Location loc = op.getLoc();
785 bool vtableUsesRelativeLayout = op.getRelativeLayout();
790 mlir::Type vtableElemTy;
791 uint64_t vtableElemAlign;
792 if (vtableUsesRelativeLayout) {
794 cir::IntType::get(builder.getContext(), 32,
true);
798 vtableElemAlign = llvm::divideCeil(
802 mlir::Type vtableElemPtrTy = cir::PointerType::get(vtableElemTy);
803 mlir::Type i64Ty = cir::IntType::get(builder.getContext(), 64,
809 auto vptrPtr = cir::VTableGetVPtrOp::create(builder, loc, op.getSrc());
810 mlir::Value vptr = cir::LoadOp::create(
811 builder, loc, vptrPtr,
815 builder.getI64IntegerAttr(vtableElemAlign),
816 cir::SyncScopeKindAttr(),
819 mlir::Value elementPtr = cir::CastOp::create(builder, loc, vtableElemPtrTy,
820 cir::CastKind::bitcast, vptr);
821 mlir::Value minusTwo =
822 cir::ConstantOp::create(builder, loc, cir::IntAttr::get(i64Ty, -2));
823 mlir::Value offsetToTopSlotPtr = cir::PtrStrideOp::create(
824 builder, loc, vtableElemPtrTy, elementPtr, minusTwo);
825 mlir::Value offsetToTop = cir::LoadOp::create(
826 builder, loc, offsetToTopSlotPtr,
830 builder.getI64IntegerAttr(vtableElemAlign),
831 cir::SyncScopeKindAttr(),
838 cir::PointerType::get(cir::IntType::get(builder.getContext(), 8,
840 op.getSrc().getType().getAddrSpace());
841 mlir::Value srcBytePtr = cir::CastOp::create(
842 builder, loc, u8PtrTy, cir::CastKind::bitcast, op.getSrc());
844 cir::PtrStrideOp::create(builder, loc, u8PtrTy, srcBytePtr, offsetToTop);
846 return cir::CastOp::create(builder, loc, op.getType(), cir::CastKind::bitcast,
851LowerItaniumCXXABI::lowerDynamicCast(cir::DynamicCastOp op,
852 mlir::OpBuilder &builder)
const {
853 mlir::Location loc = op->getLoc();
854 mlir::Value srcValue = op.getSrc();
859 return buildDynamicCastAfterNullCheck(op, builder);
861 mlir::Value srcValueIsNotNull = cir::CastOp::create(
862 builder, loc, cir::BoolType::get(builder.getContext()),
863 cir::CastKind::ptr_to_bool, srcValue);
864 return cir::TernaryOp::create(
865 builder, loc, srcValueIsNotNull,
866 [&](mlir::OpBuilder &, mlir::Location) {
867 mlir::Value castedValue =
869 ? buildDynamicCastToVoidAfterNullCheck(op, lm, builder)
870 : buildDynamicCastAfterNullCheck(op, builder);
871 cir::YieldOp::create(builder, loc, castedValue);
873 [&](mlir::OpBuilder &, mlir::Location) {
874 mlir::Value null = cir::ConstantOp::create(
876 cir::ConstPtrAttr::get(op.getType(),
877 builder.getI64IntegerAttr(0)));
878 cir::YieldOp::create(builder, loc, null);
883LowerItaniumCXXABI::lowerVTableGetTypeInfo(cir::VTableGetTypeInfoOp op,
884 mlir::OpBuilder &builder)
const {
885 mlir::Location loc = op->getLoc();
886 auto offset = cir::ConstantOp::create(
890 auto vptrCast = cir::CastOp::create(builder, loc, op.getType(),
891 cir::CastKind::bitcast, op.getVptr());
893 return cir::PtrStrideOp::create(builder, loc, vptrCast.getType(), vptrCast,
898clang::CharUnits LowerItaniumCXXABI::getArrayCookieSizeImpl(
899 clang::CharUnits elementAlign)
const {
902 clang::CharUnits sizeOfSizeT =
904 return std::max(sizeOfSizeT, elementAlign);
907mlir::Value LowerItaniumCXXABI::readArrayCookieImpl(
908 mlir::Location loc, mlir::Value allocPtr, clang::CharUnits cookieSize,
909 clang::CharUnits cookieAlignment,
const mlir::DataLayout &dataLayout,
910 CIRBaseBuilderTy &builder)
const {
911 unsigned ptrSizeInBits = getPtrSizeInBits();
912 mlir::ptr::MemorySpaceAttrInterface addrSpace =
913 mlir::cast<cir::PointerType>(allocPtr.getType()).getAddrSpace();
915 auto ptrDiffTy = builder.
getSIntNTy(ptrSizeInBits);
916 auto sizeTy = builder.
getUIntNTy(ptrSizeInBits);
919 clang::CharUnits sizeOfSizeT =
921 clang::CharUnits countOffset = cookieSize - sizeOfSizeT;
923 mlir::Value countBytePtr = allocPtr;
924 clang::CharUnits countAlignment = cookieAlignment;
925 if (!countOffset.
isZero()) {
926 mlir::Value offsetVal = cir::ConstantOp::create(
927 builder, loc, cir::IntAttr::get(ptrDiffTy, countOffset.
getQuantity()));
929 cir::PtrStrideOp::create(builder, loc, u8PtrTy, allocPtr, offsetVal);
933 auto countPtrTy = cir::PointerType::get(sizeTy, addrSpace);
934 mlir::Value countPtr = cir::CastOp::create(
935 builder, loc, countPtrTy, cir::CastKind::bitcast, countBytePtr);
936 return cir::LoadOp::create(
937 builder, loc, countPtr,
false,
false,
939 builder.getI64IntegerAttr(countAlignment.
getQuantity()),
940 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 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)
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_bitreverse32 uint32_t __builtin_riscv_aaddu_u32 __builtin_riscv_asubu_u32 __builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t uint32_t uint32_t __builtin_riscv_mhacc_i32 __builtin_riscv_mhaccu_u32 __builtin_riscv_mhaccsu_i32 __builtin_riscv_mulq_i32 int64_t
__builtin_bitreverse32 uint32_t __builtin_riscv_aaddu_u32 __builtin_riscv_asubu_u32 __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 ----------------------------—===//