| 1 | //===----- CIRCXXABI.h - Interface to C++ ABIs for CIR Dialect --*- C++ -*-===// |
| 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 file partially mimics the CodeGen/CGCXXABI.h class. The main difference |
| 10 | // is that this is adapted to operate on the CIR dialect. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_CIRCXXABI_H |
| 15 | #define CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_CIRCXXABI_H |
| 16 | |
| 17 | #include "mlir/Transforms/DialectConversion.h" |
| 18 | #include "clang/AST/CharUnits.h" |
| 19 | #include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" |
| 20 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 21 | #include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 22 | |
| 23 | namespace cir { |
| 24 | |
| 25 | // Forward declarations. |
| 26 | class LowerModule; |
| 27 | |
| 28 | class CIRCXXABI { |
| 29 | friend class LowerModule; |
| 30 | |
| 31 | protected: |
| 32 | LowerModule &lm; |
| 33 | |
| 34 | CIRCXXABI(LowerModule &lm) : lm(lm) {} |
| 35 | |
| 36 | unsigned getPtrSizeInBits() const; |
| 37 | |
| 38 | public: |
| 39 | virtual ~CIRCXXABI(); |
| 40 | |
| 41 | /// Lower the given data member pointer type to its ABI type. The returned |
| 42 | /// type is also a CIR type. |
| 43 | virtual mlir::Type |
| 44 | lowerDataMemberType(cir::DataMemberType type, |
| 45 | const mlir::TypeConverter &typeConverter) const = 0; |
| 46 | |
| 47 | /// Lower the given member function pointer type to its ABI type. The returned |
| 48 | /// type is also a CIR type. |
| 49 | virtual mlir::Type |
| 50 | lowerMethodType(cir::MethodType type, |
| 51 | const mlir::TypeConverter &typeConverter) const = 0; |
| 52 | |
| 53 | /// Lower the given data member pointer constant to a constant of the ABI |
| 54 | /// type. The returned constant is represented as an attribute as well. |
| 55 | virtual mlir::TypedAttr |
| 56 | lowerDataMemberConstant(cir::DataMemberAttr attr, |
| 57 | const mlir::DataLayout &layout, |
| 58 | const mlir::TypeConverter &typeConverter) const = 0; |
| 59 | |
| 60 | /// Lower the given by-offset data member pointer constant (used for members |
| 61 | /// with no CIR field index, e.g. no_unique_address empty fields) to a |
| 62 | /// constant of the ABI type. |
| 63 | virtual mlir::TypedAttr lowerDataMemberOffsetConstant( |
| 64 | cir::DataMemberOffsetAttr attr, const mlir::DataLayout &layout, |
| 65 | const mlir::TypeConverter &typeConverter) const = 0; |
| 66 | |
| 67 | /// Lower the given member function pointer constant to a constant of the ABI |
| 68 | /// type. The returned constant is represented as an attribute as well. |
| 69 | virtual mlir::TypedAttr |
| 70 | lowerMethodConstant(cir::MethodAttr attr, const mlir::DataLayout &layout, |
| 71 | const mlir::TypeConverter &typeConverter) const = 0; |
| 72 | |
| 73 | /// Lower the given cir.get_runtime_member op to a sequence of more |
| 74 | /// "primitive" CIR operations that act on the ABI types. |
| 75 | virtual mlir::Operation * |
| 76 | lowerGetRuntimeMember(cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy, |
| 77 | mlir::Value loweredAddr, mlir::Value loweredMember, |
| 78 | mlir::OpBuilder &builder) const = 0; |
| 79 | |
| 80 | /// Lower the given cir.get_method op to a sequence of more "primitive" CIR |
| 81 | /// operations that act on the ABI types. The lowered result values will be |
| 82 | /// stored in the given loweredResults array. |
| 83 | virtual void |
| 84 | lowerGetMethod(cir::GetMethodOp op, mlir::Value &callee, mlir::Value &thisArg, |
| 85 | mlir::Value loweredMethod, mlir::Value loweredObjectPtr, |
| 86 | mlir::ConversionPatternRewriter &rewriter) const = 0; |
| 87 | |
| 88 | /// Lower the given cir.base_data_member op to a sequence of more "primitive" |
| 89 | /// CIR operations that act on the ABI types. |
| 90 | virtual mlir::Value lowerBaseDataMember(cir::BaseDataMemberOp op, |
| 91 | mlir::Value loweredSrc, |
| 92 | mlir::OpBuilder &builder) const = 0; |
| 93 | |
| 94 | /// Lower the given cir.derived_data_member op to a sequence of more |
| 95 | /// "primitive" CIR operations that act on the ABI types. |
| 96 | virtual mlir::Value |
| 97 | lowerDerivedDataMember(cir::DerivedDataMemberOp op, mlir::Value loweredSrc, |
| 98 | mlir::OpBuilder &builder) const = 0; |
| 99 | |
| 100 | virtual mlir::Value lowerBaseMethod(cir::BaseMethodOp op, |
| 101 | mlir::Value loweredSrc, |
| 102 | mlir::OpBuilder &builder) const = 0; |
| 103 | |
| 104 | virtual mlir::Value lowerDerivedMethod(cir::DerivedMethodOp op, |
| 105 | mlir::Value loweredSrc, |
| 106 | mlir::OpBuilder &builder) const = 0; |
| 107 | |
| 108 | virtual mlir::Value lowerDataMemberCmp(cir::CmpOp op, mlir::Value loweredLhs, |
| 109 | mlir::Value loweredRhs, |
| 110 | mlir::OpBuilder &builder) const = 0; |
| 111 | |
| 112 | virtual mlir::Value lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs, |
| 113 | mlir::Value loweredRhs, |
| 114 | mlir::OpBuilder &builder) const = 0; |
| 115 | |
| 116 | virtual mlir::Value |
| 117 | lowerDataMemberBitcast(cir::CastOp op, mlir::Type loweredDstTy, |
| 118 | mlir::Value loweredSrc, |
| 119 | mlir::OpBuilder &builder) const = 0; |
| 120 | |
| 121 | virtual mlir::Value |
| 122 | lowerDataMemberToBoolCast(cir::CastOp op, mlir::Value loweredSrc, |
| 123 | mlir::OpBuilder &builder) const = 0; |
| 124 | |
| 125 | virtual mlir::Value lowerMethodBitcast(cir::CastOp op, |
| 126 | mlir::Type loweredDstTy, |
| 127 | mlir::Value loweredSrc, |
| 128 | mlir::OpBuilder &builder) const = 0; |
| 129 | |
| 130 | virtual mlir::Value lowerMethodToBoolCast(cir::CastOp op, |
| 131 | mlir::Value loweredSrc, |
| 132 | mlir::OpBuilder &builder) const = 0; |
| 133 | |
| 134 | virtual mlir::Value lowerDynamicCast(cir::DynamicCastOp op, |
| 135 | mlir::OpBuilder &builder) const = 0; |
| 136 | |
| 137 | virtual mlir::Value |
| 138 | lowerVTableGetTypeInfo(cir::VTableGetTypeInfoOp op, |
| 139 | mlir::OpBuilder &builder) const = 0; |
| 140 | |
| 141 | /// Read the array cookie for a dynamically-allocated array whose first |
| 142 | /// element is at \p elementPtr. \p elementAlign is the element type's |
| 143 | /// preferred alignment in bytes. Returns the number of elements, the original |
| 144 | /// allocation pointer (before the cookie) as a void*, and the cookie size in |
| 145 | /// bytes. Delegates to getArrayCookieSizeImpl and readArrayCookieImpl. |
| 146 | void readArrayCookie(mlir::Location loc, mlir::Value elementPtr, |
| 147 | clang::CharUnits elementAlign, |
| 148 | const mlir::DataLayout &dataLayout, |
| 149 | CIRBaseBuilderTy &builder, mlir::Value &numElements, |
| 150 | mlir::Value &allocPtr, |
| 151 | clang::CharUnits &cookieSize) const; |
| 152 | |
| 153 | protected: |
| 154 | /// Returns the cookie size in bytes for a dynamically-allocated array of |
| 155 | /// elements with the given preferred alignment. Only called when a cookie |
| 156 | /// is required. |
| 157 | virtual clang::CharUnits |
| 158 | getArrayCookieSizeImpl(clang::CharUnits elementAlign) const = 0; |
| 159 | |
| 160 | /// Reads the element count from an array cookie. \p allocPtr is a byte |
| 161 | /// pointer to the start of the allocation (the beginning of the cookie). |
| 162 | /// \p cookieSize is the value returned by getArrayCookieSizeImpl. |
| 163 | /// \p cookieAlignment is the alignment at the cookie start, derived from |
| 164 | /// the element type's ABI alignment. |
| 165 | virtual mlir::Value readArrayCookieImpl(mlir::Location loc, |
| 166 | mlir::Value allocPtr, |
| 167 | clang::CharUnits cookieSize, |
| 168 | clang::CharUnits cookieAlignment, |
| 169 | const mlir::DataLayout &dataLayout, |
| 170 | CIRBaseBuilderTy &builder) const = 0; |
| 171 | }; |
| 172 | |
| 173 | /// Creates an Itanium-family ABI. |
| 174 | std::unique_ptr<CIRCXXABI> createItaniumCXXABI(LowerModule &lm); |
| 175 | |
| 176 | /// Creates a Microsoft-family ABI. |
| 177 | std::unique_ptr<CIRCXXABI> createMicrosoftCXXABI(LowerModule &lm); |
| 178 | |
| 179 | } // namespace cir |
| 180 | |
| 181 | #endif // CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_CIRCXXABI_H |
| 182 | |