clang 23.0.0git
LowerItaniumCXXABI.cpp
Go to the documentation of this file.
1//===---- LowerItaniumCXXABI.cpp - Emit CIR code Itanium-specific code ---===//
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 provides CIR lowering logic targeting the Itanium C++ ABI. The class in
10// this file generates records that follow the Itanium C++ ABI, which is
11// documented at:
12// https://itanium-cxx-abi.github.io/cxx-abi/abi.html
13// https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
14//
15// It also supports the closely-related ARM ABI, documented at:
16// https://developer.arm.com/documentation/ihi0041/g/
17//
18// This file partially mimics clang/lib/CodeGen/ItaniumCXXABI.cpp. The queries
19// are adapted to operate on the CIR dialect, however.
20//
21//===----------------------------------------------------------------------===//
22
23#include "CIRCXXABI.h"
24#include "LowerModule.h"
25#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
26#include "mlir/IR/ImplicitLocOpBuilder.h"
27#include "llvm/Support/ErrorHandling.h"
28
29namespace cir {
30
31namespace {
32
33class LowerItaniumCXXABI : public CIRCXXABI {
34protected:
35 bool useARMMethodPtrABI;
36 bool use32BitVTableOffsetABI;
37
38public:
39 LowerItaniumCXXABI(LowerModule &lm, bool useARMMethodPtrABI = false,
40 bool use32BitVTableOffsetABI = false)
41 : CIRCXXABI(lm), useARMMethodPtrABI(useARMMethodPtrABI),
42 use32BitVTableOffsetABI(use32BitVTableOffsetABI) {}
43
44 /// Lower the given data member pointer type to its ABI type. The returned
45 /// type is also a CIR type.
46 virtual mlir::Type
47 lowerDataMemberType(cir::DataMemberType type,
48 const mlir::TypeConverter &typeConverter) const override;
49
50 mlir::Type
51 lowerMethodType(cir::MethodType type,
52 const mlir::TypeConverter &typeConverter) const override;
53
54 mlir::TypedAttr lowerDataMemberConstant(
55 cir::DataMemberAttr attr, const mlir::DataLayout &layout,
56 const mlir::TypeConverter &typeConverter) const override;
57
58 mlir::TypedAttr
59 lowerMethodConstant(cir::MethodAttr attr, const mlir::DataLayout &layout,
60 const mlir::TypeConverter &typeConverter) const override;
61
62 mlir::Operation *
63 lowerGetRuntimeMember(cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
64 mlir::Value loweredAddr, mlir::Value loweredMember,
65 mlir::OpBuilder &builder) const override;
66
67 void lowerGetMethod(cir::GetMethodOp op, mlir::Value &callee,
68 mlir::Value &thisArg, mlir::Value loweredMethod,
69 mlir::Value loweredObjectPtr,
70 mlir::ConversionPatternRewriter &rewriter) const override;
71
72 mlir::Value lowerBaseDataMember(cir::BaseDataMemberOp op,
73 mlir::Value loweredSrc,
74 mlir::OpBuilder &builder) const override;
75
76 mlir::Value lowerDerivedDataMember(cir::DerivedDataMemberOp op,
77 mlir::Value loweredSrc,
78 mlir::OpBuilder &builder) const override;
79
80 mlir::Value lowerBaseMethod(cir::BaseMethodOp op, mlir::Value loweredSrc,
81 mlir::OpBuilder &builder) const override;
82
83 mlir::Value lowerDerivedMethod(cir::DerivedMethodOp op,
84 mlir::Value loweredSrc,
85 mlir::OpBuilder &builder) const override;
86
87 mlir::Value lowerDataMemberCmp(cir::CmpOp op, mlir::Value loweredLhs,
88 mlir::Value loweredRhs,
89 mlir::OpBuilder &builder) const override;
90
91 mlir::Value lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs,
92 mlir::Value loweredRhs,
93 mlir::OpBuilder &builder) const override;
94
95 mlir::Value lowerDataMemberBitcast(cir::CastOp op, mlir::Type loweredDstTy,
96 mlir::Value loweredSrc,
97 mlir::OpBuilder &builder) const override;
98
99 mlir::Value
100 lowerDataMemberToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
101 mlir::OpBuilder &builder) const override;
102
103 mlir::Value lowerMethodBitcast(cir::CastOp op, mlir::Type loweredDstTy,
104 mlir::Value loweredSrc,
105 mlir::OpBuilder &builder) const override;
106
107 mlir::Value lowerMethodToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
108 mlir::OpBuilder &builder) const override;
109
110 mlir::Value lowerDynamicCast(cir::DynamicCastOp op,
111 mlir::OpBuilder &builder) const override;
112 mlir::Value lowerVTableGetTypeInfo(cir::VTableGetTypeInfoOp op,
113 mlir::OpBuilder &builder) const override;
114
115 clang::CharUnits
116 getArrayCookieSizeImpl(mlir::Type elementType,
117 const mlir::DataLayout &dataLayout) const override;
118
119 mlir::Value readArrayCookieImpl(mlir::Location loc, mlir::Value allocPtr,
120 clang::CharUnits cookieSize,
121 clang::CharUnits cookieAlignment,
122 const mlir::DataLayout &dataLayout,
123 CIRBaseBuilderTy &builder) const override;
124};
125
126} // namespace
127
128std::unique_ptr<CIRCXXABI> createItaniumCXXABI(LowerModule &lm) {
129 switch (lm.getCXXABIKind()) {
130 // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
131 // include the other 32-bit ARM oddities: constructor/destructor return values
132 // and array cookies.
133 case clang::TargetCXXABI::GenericAArch64:
134 return std::make_unique<LowerItaniumCXXABI>(
135 lm,
136 /*useARMMethodPtrABI=*/true,
137 /*use32BitVTableOffsetABI=*/false);
138 case clang::TargetCXXABI::AppleARM64:
139 // TODO: this isn't quite right, clang uses AppleARM64CXXABI which inherits
140 // from ARMCXXABI. We'll have to follow suit.
142 return std::make_unique<LowerItaniumCXXABI>(
143 lm,
144 /*useARMMethodPtrABI=*/true,
145 /*use32BitVTableOffsetABI=*/true);
146
147 case clang::TargetCXXABI::GenericItanium:
148 return std::make_unique<LowerItaniumCXXABI>(lm);
149
150 case clang::TargetCXXABI::Microsoft:
151 llvm_unreachable("Microsoft ABI is not Itanium-based");
152 default:
153 llvm_unreachable("Other Itanium ABI?");
154 }
155}
156
157static cir::IntType getPtrDiffCIRTy(LowerModule &lm) {
158 const clang::TargetInfo &target = lm.getTarget();
161 return cir::IntType::get(lm.getMLIRContext(), target.getTypeWidth(ptrdiffTy),
162 target.isTypeSigned(ptrdiffTy));
163}
164
165mlir::Type LowerItaniumCXXABI::lowerDataMemberType(
166 cir::DataMemberType type, const mlir::TypeConverter &typeConverter) const {
167 // Itanium C++ ABI 2.3.1:
168 // A data member pointer is represented as the data member's offset in bytes
169 // from the address point of an object of the base type, as a ptrdiff_t.
170 return getPtrDiffCIRTy(lm);
171}
172
173mlir::Type LowerItaniumCXXABI::lowerMethodType(
174 cir::MethodType type, const mlir::TypeConverter &typeConverter) const {
175 // Itanium C++ ABI 2.3.2:
176 // In all representations, the basic ABI properties of member function
177 // pointer types are those of the following class, where fnptr_t is the
178 // appropriate function-pointer type for a member function of this type:
179 //
180 // struct {
181 // fnptr_t ptr;
182 // ptrdiff_t adj;
183 // };
184
185 cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(lm);
186
187 // Note that clang CodeGen emits struct{ptrdiff_t, ptrdiff_t} for member
188 // function pointers. Let's follow this approach.
189 return cir::StructType::get(type.getContext(), {ptrdiffCIRTy, ptrdiffCIRTy},
190 /*packed=*/false, /*padded=*/false,
191 /*is_class=*/false);
192}
193
194mlir::TypedAttr LowerItaniumCXXABI::lowerDataMemberConstant(
195 cir::DataMemberAttr attr, const mlir::DataLayout &layout,
196 const mlir::TypeConverter &typeConverter) const {
197 int64_t memberOffset;
198 if (attr.isNullPtr()) {
199 // Itanium C++ ABI 2.3:
200 // A NULL pointer is represented as -1.
201 memberOffset = -1;
202 } else {
203 // Itanium C++ ABI 2.3:
204 // A pointer to data member is an offset from the base address of
205 // the class object containing it, represented as a ptrdiff_t
206 unsigned memberIndex = attr.getMemberIndex().value();
207 memberOffset =
208 attr.getType().getClassTy().getElementOffset(layout, memberIndex);
209 }
210
211 mlir::Type abiTy = lowerDataMemberType(attr.getType(), typeConverter);
212 return cir::IntAttr::get(abiTy, memberOffset);
213}
214
215mlir::TypedAttr LowerItaniumCXXABI::lowerMethodConstant(
216 cir::MethodAttr attr, const mlir::DataLayout &layout,
217 const mlir::TypeConverter &typeConverter) const {
218 cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(lm);
219
220 auto loweredMethodTy = mlir::cast<cir::StructType>(
221 lowerMethodType(attr.getType(), typeConverter));
222
223 auto zero = cir::IntAttr::get(ptrdiffCIRTy, 0);
224
225 // Itanium C++ ABI 2.3.2:
226 // In all representations, the basic ABI properties of member function
227 // pointer types are those of the following class, where fnptr_t is the
228 // appropriate function-pointer type for a member function of this type:
229 //
230 // struct {
231 // fnptr_t ptr;
232 // ptrdiff_t adj;
233 // };
234
235 if (attr.isNull()) {
236 // Itanium C++ ABI 2.3.2:
237 //
238 // In the standard representation, a null member function pointer is
239 // represented with ptr set to a null pointer. The value of adj is
240 // unspecified for null member function pointers.
241 //
242 // clang CodeGen emits struct{null, null} for null member function pointers.
243 // Let's do the same here.
244 return cir::ConstRecordAttr::get(
245 loweredMethodTy, mlir::ArrayAttr::get(attr.getContext(), {zero, zero}));
246 }
247
248 if (attr.isVirtual()) {
249 if (useARMMethodPtrABI) {
250 // ARM C++ ABI 3.2.1:
251 // This ABI specifies that adj contains twice the this
252 // adjustment, plus 1 if the member function is virtual. The
253 // least significant bit of adj then makes exactly the same
254 // discrimination as the least significant bit of ptr does for
255 // Itanium.
257 auto ptr =
258 cir::IntAttr::get(ptrdiffCIRTy, attr.getVtableOffset().value());
259 auto one = cir::IntAttr::get(ptrdiffCIRTy, 1);
260 return cir::ConstRecordAttr::get(
261 loweredMethodTy, mlir::ArrayAttr::get(attr.getContext(), {ptr, one}));
262 }
263
264 // Itanium C++ ABI 2.3.2:
265 //
266 // In the standard representation, a member function pointer for a
267 // virtual function is represented with ptr set to 1 plus the function's
268 // v-table entry offset (in bytes), converted to a function pointer as if
269 // by reinterpret_cast<fnptr_t>(uintfnptr_t(1 + offset)), where
270 // uintfnptr_t is an unsigned integer of the same size as fnptr_t.
271 auto ptr =
272 cir::IntAttr::get(ptrdiffCIRTy, 1 + attr.getVtableOffset().value());
273 return cir::ConstRecordAttr::get(
274 loweredMethodTy, mlir::ArrayAttr::get(attr.getContext(), {ptr, zero}));
275 }
276
277 // Itanium C++ ABI 2.3.2:
278 //
279 // A member function pointer for a non-virtual member function is
280 // represented with ptr set to a pointer to the function, using the base
281 // ABI's representation of function pointers.
282 auto ptr = cir::GlobalViewAttr::get(ptrdiffCIRTy, attr.getSymbol().value());
283 return cir::ConstRecordAttr::get(
284 loweredMethodTy, mlir::ArrayAttr::get(attr.getContext(), {ptr, zero}));
285}
286
287mlir::Operation *LowerItaniumCXXABI::lowerGetRuntimeMember(
288 cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
289 mlir::Value loweredAddr, mlir::Value loweredMember,
290 mlir::OpBuilder &builder) const {
291 auto byteTy = cir::IntType::get(op.getContext(), 8, true);
292 auto bytePtrTy = cir::PointerType::get(
293 byteTy,
294 mlir::cast<cir::PointerType>(op.getAddr().getType()).getAddrSpace());
295 auto objectBytesPtr = cir::CastOp::create(
296 builder, op.getLoc(), bytePtrTy, cir::CastKind::bitcast, op.getAddr());
297 auto memberBytesPtr = cir::PtrStrideOp::create(
298 builder, op.getLoc(), bytePtrTy, objectBytesPtr, loweredMember);
299 return cir::CastOp::create(builder, op.getLoc(), op.getType(),
300 cir::CastKind::bitcast, memberBytesPtr);
301}
302
303void LowerItaniumCXXABI::lowerGetMethod(
304 cir::GetMethodOp op, mlir::Value &callee, mlir::Value &thisArg,
305 mlir::Value loweredMethod, mlir::Value loweredObjectPtr,
306 mlir::ConversionPatternRewriter &rewriter) const {
307 // In the Itanium and ARM ABIs, method pointers have the form:
308 // struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
309 //
310 // In the Itanium ABI:
311 // - method pointers are virtual if (memptr.ptr & 1) is nonzero
312 // - the this-adjustment is (memptr.adj)
313 // - the virtual offset is (memptr.ptr - 1)
314 //
315 // In the ARM ABI:
316 // - method pointers are virtual if (memptr.adj & 1) is nonzero
317 // - the this-adjustment is (memptr.adj >> 1)
318 // - the virtual offset is (memptr.ptr)
319 // ARM uses 'adj' for the virtual flag because Thumb functions
320 // may be only single-byte aligned.
321 //
322 // If the member is virtual, the adjusted 'this' pointer points
323 // to a vtable pointer from which the virtual offset is applied.
324 //
325 // If the member is non-virtual, memptr.ptr is the address of
326 // the function to call.
327
328 mlir::ImplicitLocOpBuilder locBuilder(op.getLoc(), rewriter);
329 mlir::Type calleePtrTy = op.getCallee().getType();
330
331 cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(lm);
332 mlir::Value ptrdiffOne =
333 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 1));
334
335 mlir::Value rawAdj =
336 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredMethod, 1);
337 mlir::Value adj = rawAdj;
338 if (useARMMethodPtrABI)
339 adj = cir::ShiftOp::create(locBuilder, ptrdiffCIRTy, adj, ptrdiffOne,
340 /*isLeftShift=*/false);
341
342 // Apply the adjustment to the 'this' pointer.
343 mlir::Type thisVoidPtrTy =
344 cir::PointerType::get(cir::VoidType::get(locBuilder.getContext()),
345 op.getObject().getType().getAddrSpace());
346 mlir::Value thisVoidPtr = cir::CastOp::create(
347 locBuilder, thisVoidPtrTy, cir::CastKind::bitcast, loweredObjectPtr);
348 thisArg =
349 cir::PtrStrideOp::create(locBuilder, thisVoidPtrTy, thisVoidPtr, adj);
350
351 // Load the "ptr" field of the member function pointer and determine if it
352 // points to a virtual function.
353 mlir::Value methodPtrField =
354 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredMethod, 0);
355 mlir::Value virtualBit;
356 if (useARMMethodPtrABI)
357 virtualBit = cir::AndOp::create(locBuilder, rawAdj, ptrdiffOne);
358 else
359 virtualBit = cir::AndOp::create(locBuilder, methodPtrField, ptrdiffOne);
360 mlir::Value isVirtual = cir::CmpOp::create(locBuilder, cir::CmpOpKind::eq,
361 virtualBit, ptrdiffOne);
362
366
367 auto buildVirtualCallee = [&](mlir::OpBuilder &b, mlir::Location loc) {
368 // Load vtable pointer.
369 // Note that vtable pointer always point to the global address space.
370 auto vtablePtrTy =
371 cir::PointerType::get(cir::IntType::get(b.getContext(), 8, true));
372 auto vtablePtrPtrTy = cir::PointerType::get(
373 vtablePtrTy, op.getObject().getType().getAddrSpace());
374 auto vtablePtrPtr = cir::CastOp::create(b, loc, vtablePtrPtrTy,
375 cir::CastKind::bitcast, thisArg);
377 mlir::Value vtablePtr =
378 cir::LoadOp::create(b, loc, vtablePtrPtr, /*isDeref=*/false,
379 /*isVolatile=*/false,
380 /*alignment=*/mlir::IntegerAttr(),
381 /*sync_scope=*/cir::SyncScopeKindAttr{},
382 /*mem_order=*/cir::MemOrderAttr());
383
384 // Apply the offset.
385 // On ARM64, to reserve extra space in virtual member function pointers,
386 // we only pay attention to the low 32 bits of the offset.
387 mlir::Value vtableOffset = methodPtrField;
388 if (!useARMMethodPtrABI)
389 vtableOffset = cir::SubOp::create(b, loc, vtableOffset.getType(),
390 vtableOffset, ptrdiffOne);
391 if (use32BitVTableOffsetABI)
392 llvm_unreachable("AppleARM64 method ptr abi NYI");
393
397
398 // Apply the offset to the vtable pointer and get the pointer to the target
399 // virtual function. Then load that pointer to get the callee.
400 mlir::Value vfpAddr = cir::PtrStrideOp::create(locBuilder, vtablePtrTy,
401 vtablePtr, vtableOffset);
402 auto vfpPtrTy = cir::PointerType::get(calleePtrTy);
403 mlir::Value vfpPtr = cir::CastOp::create(locBuilder, vfpPtrTy,
404 cir::CastKind::bitcast, vfpAddr);
405 auto fnPtr = cir::LoadOp::create(b, loc, vfpPtr,
406 /*isDeref=*/false, /*isVolatile=*/false,
407 /*alignment=*/mlir::IntegerAttr(),
408 /*sync_scope=*/cir::SyncScopeKindAttr{},
409 /*mem_order=*/cir::MemOrderAttr());
410
411 cir::YieldOp::create(b, loc, fnPtr.getResult());
413 };
414
415 callee = cir::TernaryOp::create(
416 locBuilder, isVirtual, /*thenBuilder=*/buildVirtualCallee,
417 /*elseBuilder=*/
418 [&](mlir::OpBuilder &b, mlir::Location loc) {
419 auto fnPtr = cir::CastOp::create(b, loc, calleePtrTy,
420 cir::CastKind::int_to_ptr,
421 methodPtrField);
422 cir::YieldOp::create(b, loc, fnPtr.getResult());
423 })
424 .getResult();
425}
426
427static mlir::Value lowerDataMemberCast(mlir::Operation *op,
428 mlir::Value loweredSrc,
429 std::int64_t offset,
430 bool isDerivedToBase,
431 mlir::OpBuilder &builder) {
432 if (offset == 0)
433 return loweredSrc;
434 mlir::Location loc = op->getLoc();
435 mlir::Type ty = loweredSrc.getType();
436
437 auto getConstantInt = [&](int64_t value) -> cir::ConstantOp {
438 return cir::ConstantOp::create(builder, loc, cir::IntAttr::get(ty, value));
439 };
440
441 cir::ConstantOp nullValue = getConstantInt(-1);
442 auto isNull = cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, loweredSrc,
443 nullValue);
444
445 cir::ConstantOp offsetValue = getConstantInt(offset);
446 mlir::Value adjustedPtr;
447 if (isDerivedToBase) {
448 auto subOp = cir::SubOp::create(builder, loc, ty, loweredSrc, offsetValue);
449 subOp.setNoSignedWrap(true);
450 adjustedPtr = subOp;
451 } else {
452 auto addOp = cir::AddOp::create(builder, loc, ty, loweredSrc, offsetValue);
453 addOp.setNoSignedWrap(true);
454 adjustedPtr = addOp;
455 }
456
457 return cir::SelectOp::create(builder, loc, ty, isNull, loweredSrc,
458 adjustedPtr);
459}
460
461mlir::Value
462LowerItaniumCXXABI::lowerBaseDataMember(cir::BaseDataMemberOp op,
463 mlir::Value loweredSrc,
464 mlir::OpBuilder &builder) const {
465 return lowerDataMemberCast(op, loweredSrc, op.getOffset().getSExtValue(),
466 /*isDerivedToBase=*/true, builder);
467}
468
469mlir::Value
470LowerItaniumCXXABI::lowerDerivedDataMember(cir::DerivedDataMemberOp op,
471 mlir::Value loweredSrc,
472 mlir::OpBuilder &builder) const {
473 return lowerDataMemberCast(op, loweredSrc, op.getOffset().getSExtValue(),
474 /*isDerivedToBase=*/false, builder);
475}
476
477static mlir::Value lowerMethodCast(mlir::Operation *op, mlir::Value loweredSrc,
478 std::int64_t offset, bool isDerivedToBase,
479 bool useARMMethodPtrABI,
480 LowerModule &lowerMod,
481 mlir::OpBuilder &builder) {
482 if (offset == 0)
483 return loweredSrc;
484
485 // The this-adjustment is left-shifted by 1 on ARM, since the low bit of the
486 // adjustment field is used to encode whether the member function is virtual.
487 if (useARMMethodPtrABI)
488 offset <<= 1;
489
490 cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(lowerMod);
491 auto adjField = cir::ExtractMemberOp::create(builder, op->getLoc(),
492 ptrdiffCIRTy, loweredSrc, 1);
493
494 auto offsetValue = cir::ConstantOp::create(
495 builder, op->getLoc(), cir::IntAttr::get(ptrdiffCIRTy, offset));
496 mlir::Value adjustedAdjField;
497 if (isDerivedToBase) {
498 auto subOp = cir::SubOp::create(builder, op->getLoc(), ptrdiffCIRTy,
499 adjField, offsetValue);
500 subOp.setNoSignedWrap(true);
501 adjustedAdjField = subOp;
502 } else {
503 auto addOp = cir::AddOp::create(builder, op->getLoc(), ptrdiffCIRTy,
504 adjField, offsetValue);
505 addOp.setNoSignedWrap(true);
506 adjustedAdjField = addOp;
507 }
508
509 return cir::InsertMemberOp::create(builder, op->getLoc(), loweredSrc, 1,
510 adjustedAdjField);
511}
512
513mlir::Value
514LowerItaniumCXXABI::lowerBaseMethod(cir::BaseMethodOp op,
515 mlir::Value loweredSrc,
516 mlir::OpBuilder &builder) const {
517 return lowerMethodCast(op, loweredSrc, op.getOffset().getSExtValue(),
518 /*isDerivedToBase=*/true, useARMMethodPtrABI, lm,
519 builder);
520}
521
522mlir::Value
523LowerItaniumCXXABI::lowerDerivedMethod(cir::DerivedMethodOp op,
524 mlir::Value loweredSrc,
525 mlir::OpBuilder &builder) const {
526 return lowerMethodCast(op, loweredSrc, op.getOffset().getSExtValue(),
527 /*isDerivedToBase=*/false, useARMMethodPtrABI, lm,
528 builder);
529}
530
531mlir::Value
532LowerItaniumCXXABI::lowerDataMemberCmp(cir::CmpOp op, mlir::Value loweredLhs,
533 mlir::Value loweredRhs,
534 mlir::OpBuilder &builder) const {
535 return cir::CmpOp::create(builder, op.getLoc(), op.getKind(), loweredLhs,
536 loweredRhs);
537}
538
539mlir::Value LowerItaniumCXXABI::lowerMethodCmp(cir::CmpOp op,
540 mlir::Value loweredLhs,
541 mlir::Value loweredRhs,
542 mlir::OpBuilder &builder) const {
543 assert(op.getKind() == cir::CmpOpKind::eq ||
544 op.getKind() == cir::CmpOpKind::ne);
545
546 mlir::ImplicitLocOpBuilder locBuilder(op.getLoc(), builder);
547 cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(lm);
548 mlir::Value ptrdiffZero =
549 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 0));
550
551 mlir::Value lhsPtrField =
552 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredLhs, 0);
553 mlir::Value rhsPtrField =
554 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredRhs, 0);
555 mlir::Value ptrCmp =
556 cir::CmpOp::create(locBuilder, op.getKind(), lhsPtrField, rhsPtrField);
557 mlir::Value ptrCmpToNull =
558 cir::CmpOp::create(locBuilder, op.getKind(), lhsPtrField, ptrdiffZero);
559
560 mlir::Value lhsAdjField =
561 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredLhs, 1);
562 mlir::Value rhsAdjField =
563 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredRhs, 1);
564 mlir::Value adjCmp =
565 cir::CmpOp::create(locBuilder, op.getKind(), lhsAdjField, rhsAdjField);
566
567 auto create_and = [&](mlir::Value lhs, mlir::Value rhs) {
568 return cir::AndOp::create(locBuilder, lhs.getType(), lhs, rhs);
569 };
570 auto create_or = [&](mlir::Value lhs, mlir::Value rhs) {
571 return cir::OrOp::create(locBuilder, lhs.getType(), lhs, rhs);
572 };
573
574 // Null member function pointers on ARM clear the low bit of Adj,
575 // so the zero condition has to check that neither low bit is set.
576 if (useARMMethodPtrABI) {
577 mlir::Value one =
578 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 1));
579
580 // The low bit of the adjustment field is used to encode whether the member
581 // function is virtual, but the ARM ABI specifies that for null pointers
582 // this bit must be clear. Therefore, to test whether the member pointer is
583 // null, we need to check that bit.
584 //
585 // If we are performing an equality check, ptrCmpToNull indicates that both
586 // pointers are null (if they are equal -- we only actually test lhs).
587 // If we are performing an inequality check, ptrCmpToNull indicates that
588 // one of the pointers is not null.
589 //
590 // To apply the ARM-specific logic, if either virtual bit is set, they
591 // cannot both be null (equality case -- ptrCmpToNull &= orAdjAnd1CmpZero),
592 // and if either virtual bit is set, one of the pointers is not null
593 // (inequality case -- ptrCmpToNull |= orAdjAnd1CmpZero).
594 mlir::Value orAdj = create_or(lhsAdjField, rhsAdjField);
595 mlir::Value orAdjAnd1 = create_and(orAdj, one);
596 mlir::Value orAdjAnd1CmpZero =
597 cir::CmpOp::create(locBuilder, op.getKind(), orAdjAnd1, ptrdiffZero);
598
599 if (op.getKind() == cir::CmpOpKind::eq)
600 ptrCmpToNull = create_and(ptrCmpToNull, orAdjAnd1CmpZero);
601 else
602 ptrCmpToNull = create_or(ptrCmpToNull, orAdjAnd1CmpZero);
603 }
604
605 mlir::Value result;
606 if (op.getKind() == cir::CmpOpKind::eq) {
607 // (lhs.ptr == null || lhs.adj == rhs.adj) && lhs.ptr == rhs.ptr
608 result = create_and(ptrCmp, create_or(ptrCmpToNull, adjCmp));
609 } else {
610 // lhs.ptr == rhs.ptr && (lhs.ptr == null || lhs.adj == rhs.adj)
611 result = create_or(ptrCmp, create_and(ptrCmpToNull, adjCmp));
612 }
613
614 return result;
615}
616
617mlir::Value LowerItaniumCXXABI::lowerDataMemberBitcast(
618 cir::CastOp op, mlir::Type loweredDstTy, mlir::Value loweredSrc,
619 mlir::OpBuilder &builder) const {
620 if (loweredSrc.getType() == loweredDstTy)
621 return loweredSrc;
622
623 return cir::CastOp::create(builder, op.getLoc(), loweredDstTy,
624 cir::CastKind::bitcast, loweredSrc);
625}
626
627mlir::Value LowerItaniumCXXABI::lowerDataMemberToBoolCast(
628 cir::CastOp op, mlir::Value loweredSrc, mlir::OpBuilder &builder) const {
629 // Itanium C++ ABI 2.3:
630 // A NULL pointer is represented as -1.
631 auto nullAttr = cir::IntAttr::get(getPtrDiffCIRTy(lm), -1);
632 auto nullValue = cir::ConstantOp::create(builder, op.getLoc(), nullAttr);
633 return cir::CmpOp::create(builder, op.getLoc(), cir::CmpOpKind::ne,
634 loweredSrc, nullValue);
635}
636
637mlir::Value
638LowerItaniumCXXABI::lowerMethodBitcast(cir::CastOp op, mlir::Type loweredDstTy,
639 mlir::Value loweredSrc,
640 mlir::OpBuilder &builder) const {
641 if (loweredSrc.getType() == loweredDstTy)
642 return loweredSrc;
643
644 return loweredSrc;
645}
646
647mlir::Value LowerItaniumCXXABI::lowerMethodToBoolCast(
648 cir::CastOp op, mlir::Value loweredSrc, mlir::OpBuilder &builder) const {
649 mlir::ImplicitLocOpBuilder locBuilder(op.getLoc(), builder);
650
651 // Itanium C++ ABI 2.3.2:
652 //
653 // In the standard representation, a null member function pointer is
654 // represented with ptr set to a null pointer. The value of adj is
655 // unspecified for null member function pointers.
656 cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(lm);
657 mlir::Value ptrdiffZero =
658 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 0));
659 mlir::Value ptrField =
660 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredSrc, 0);
661
662 mlir::Value result =
663 cir::CmpOp::create(locBuilder, cir::CmpOpKind::ne, ptrField, ptrdiffZero);
664
665 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
666 // (the virtual bit) is set.
667 if (useARMMethodPtrABI) {
668 mlir::Value one =
669 cir::ConstantOp::create(locBuilder, cir::IntAttr::get(ptrdiffCIRTy, 1));
670 mlir::Value adj =
671 cir::ExtractMemberOp::create(locBuilder, ptrdiffCIRTy, loweredSrc, 1);
672 mlir::Value virtualBit =
673 cir::AndOp::create(locBuilder, ptrdiffCIRTy, adj, one);
674 mlir::Value isVirtual = cir::CmpOp::create(locBuilder, cir::CmpOpKind::ne,
675 virtualBit, ptrdiffZero);
676 result = cir::OrOp::create(locBuilder, result, isVirtual);
677 }
678
679 return result;
680}
681
682static void buildBadCastCall(mlir::OpBuilder &builder, mlir::Location loc,
683 mlir::FlatSymbolRefAttr badCastFuncRef) {
684 auto callOp = cir::CallOp::create(builder, loc, badCastFuncRef,
685 /*resType=*/cir::VoidType(),
686 /*operands=*/mlir::ValueRange{});
687 callOp->setAttr(cir::CIRDialect::getNoReturnAttrName(),
688 builder.getUnitAttr());
689
690 cir::UnreachableOp::create(builder, loc);
691 builder.clearInsertionPoint();
692}
693
694static mlir::Value buildDynamicCastAfterNullCheck(cir::DynamicCastOp op,
695 mlir::OpBuilder &builder) {
696 mlir::Location loc = op->getLoc();
697 mlir::Value srcValue = op.getSrc();
698 cir::DynamicCastInfoAttr castInfo = op.getInfo().value();
699
700 // TODO(cir): consider address space
702
703 auto voidPtrTy =
704 cir::PointerType::get(cir::VoidType::get(builder.getContext()));
705
706 mlir::Value srcPtr = cir::CastOp::create(builder, loc, voidPtrTy,
707 cir::CastKind::bitcast, srcValue);
708 mlir::Value srcRtti =
709 cir::ConstantOp::create(builder, loc, castInfo.getSrcRtti());
710 mlir::Value destRtti =
711 cir::ConstantOp::create(builder, loc, castInfo.getDestRtti());
712 mlir::Value offsetHint =
713 cir::ConstantOp::create(builder, loc, castInfo.getOffsetHint());
714
715 mlir::FlatSymbolRefAttr dynCastFuncRef = castInfo.getRuntimeFunc();
716 mlir::Value dynCastFuncArgs[4] = {srcPtr, srcRtti, destRtti, offsetHint};
717
718 mlir::Value castedPtr = cir::CallOp::create(builder, loc, dynCastFuncRef,
719 voidPtrTy, dynCastFuncArgs)
720 .getResult();
721
722 assert(mlir::isa<cir::PointerType>(castedPtr.getType()) &&
723 "the return value of __dynamic_cast should be a ptr");
724
725 /// C++ [expr.dynamic.cast]p9:
726 /// A failed cast to reference type throws std::bad_cast
727 if (op.isRefCast()) {
728 // Emit a cir.if that checks the casted value.
729 mlir::Value null = cir::ConstantOp::create(
730 builder, loc,
731 cir::ConstPtrAttr::get(castedPtr.getType(),
732 builder.getI64IntegerAttr(0)));
733 mlir::Value castedPtrIsNull =
734 cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, castedPtr, null);
735 cir::IfOp::create(builder, loc, castedPtrIsNull, false,
736 [&](mlir::OpBuilder &, mlir::Location) {
737 buildBadCastCall(builder, loc,
738 castInfo.getBadCastFunc());
739 });
740 }
741
742 // Note that castedPtr is a void*. Cast it to a pointer to the destination
743 // type before return.
744 return cir::CastOp::create(builder, loc, op.getType(), cir::CastKind::bitcast,
745 castedPtr);
746}
747
749 cir::DynamicCastOp op, cir::LowerModule &lm, mlir::OpBuilder &builder) {
750 mlir::Location loc = op.getLoc();
751 bool vtableUsesRelativeLayout = op.getRelativeLayout();
752
753 // TODO(cir): consider address space in this function.
755
756 mlir::Type vtableElemTy;
757 uint64_t vtableElemAlign;
758 if (vtableUsesRelativeLayout) {
759 vtableElemTy =
760 cir::IntType::get(builder.getContext(), 32, /*isSigned=*/true);
761 vtableElemAlign = 4;
762 } else {
763 vtableElemTy = getPtrDiffCIRTy(lm);
764 vtableElemAlign = llvm::divideCeil(
766 }
767
768 mlir::Type vtableElemPtrTy = cir::PointerType::get(vtableElemTy);
769 mlir::Type i64Ty = cir::IntType::get(builder.getContext(), /*width=*/64,
770 /*isSigned=*/true);
771
772 // Access vtable to get the offset from the given object to its containing
773 // complete object.
774 // TODO: Add a specialized operation to get the object offset?
775 auto vptrPtr = cir::VTableGetVPtrOp::create(builder, loc, op.getSrc());
776 mlir::Value vptr = cir::LoadOp::create(
777 builder, loc, vptrPtr,
778 /*isDeref=*/false,
779 /*is_volatile=*/false,
780 /*alignment=*/builder.getI64IntegerAttr(vtableElemAlign),
781 /*sync_scope=*/cir::SyncScopeKindAttr(),
782 /*mem_order=*/cir::MemOrderAttr());
783 mlir::Value elementPtr = cir::CastOp::create(builder, loc, vtableElemPtrTy,
784 cir::CastKind::bitcast, vptr);
785 mlir::Value minusTwo =
786 cir::ConstantOp::create(builder, loc, cir::IntAttr::get(i64Ty, -2));
787 mlir::Value offsetToTopSlotPtr = cir::PtrStrideOp::create(
788 builder, loc, vtableElemPtrTy, elementPtr, minusTwo);
789 mlir::Value offsetToTop = cir::LoadOp::create(
790 builder, loc, offsetToTopSlotPtr,
791 /*isDeref=*/false,
792 /*is_volatile=*/false,
793 /*alignment=*/builder.getI64IntegerAttr(vtableElemAlign),
794 /*sync_scope=*/cir::SyncScopeKindAttr(),
795 /*mem_order=*/cir::MemOrderAttr());
796
797 auto voidPtrTy =
798 cir::PointerType::get(cir::VoidType::get(builder.getContext()));
799
800 // Add the offset to the given pointer to get the cast result.
801 // Cast the input pointer to a uint8_t* to allow pointer arithmetic.
802 mlir::Type u8PtrTy =
803 cir::PointerType::get(cir::IntType::get(builder.getContext(), /*width=*/8,
804 /*isSigned=*/false));
805 mlir::Value srcBytePtr = cir::CastOp::create(
806 builder, loc, u8PtrTy, cir::CastKind::bitcast, op.getSrc());
807 auto dstBytePtr =
808 cir::PtrStrideOp::create(builder, loc, u8PtrTy, srcBytePtr, offsetToTop);
809 // Cast the result to a void*.
810 return cir::CastOp::create(builder, loc, voidPtrTy, cir::CastKind::bitcast,
811 dstBytePtr);
812}
813
814mlir::Value
815LowerItaniumCXXABI::lowerDynamicCast(cir::DynamicCastOp op,
816 mlir::OpBuilder &builder) const {
817 mlir::Location loc = op->getLoc();
818 mlir::Value srcValue = op.getSrc();
819
821
822 if (op.isRefCast())
823 return buildDynamicCastAfterNullCheck(op, builder);
824
825 mlir::Value srcValueIsNotNull = cir::CastOp::create(
826 builder, loc, cir::BoolType::get(builder.getContext()),
827 cir::CastKind::ptr_to_bool, srcValue);
828 return cir::TernaryOp::create(
829 builder, loc, srcValueIsNotNull,
830 [&](mlir::OpBuilder &, mlir::Location) {
831 mlir::Value castedValue =
832 op.isCastToVoid()
833 ? buildDynamicCastToVoidAfterNullCheck(op, lm, builder)
834 : buildDynamicCastAfterNullCheck(op, builder);
835 cir::YieldOp::create(builder, loc, castedValue);
836 },
837 [&](mlir::OpBuilder &, mlir::Location) {
838 mlir::Value null = cir::ConstantOp::create(
839 builder, loc,
840 cir::ConstPtrAttr::get(op.getType(),
841 builder.getI64IntegerAttr(0)));
842 cir::YieldOp::create(builder, loc, null);
843 })
844 .getResult();
845}
846mlir::Value
847LowerItaniumCXXABI::lowerVTableGetTypeInfo(cir::VTableGetTypeInfoOp op,
848 mlir::OpBuilder &builder) const {
849 mlir::Location loc = op->getLoc();
850 auto offset = cir::ConstantOp::create(
851 builder, op->getLoc(), cir::IntAttr::get(getPtrDiffCIRTy(lm), -1));
852
853 // Cast the vptr to type_info-ptr, so that we can go backwards 1 pointer.
854 auto vptrCast = cir::CastOp::create(builder, loc, op.getType(),
855 cir::CastKind::bitcast, op.getVptr());
856
857 return cir::PtrStrideOp::create(builder, loc, vptrCast.getType(), vptrCast,
858 offset)
859 .getResult();
860}
861
862clang::CharUnits LowerItaniumCXXABI::getArrayCookieSizeImpl(
863 mlir::Type elementType, const mlir::DataLayout &dataLayout) const {
864 // The array cookie is a size_t; pad that up to the element alignment.
865 // The cookie is actually right-justified in that space.
866 clang::CharUnits sizeOfSizeT =
867 clang::CharUnits::fromQuantity(getPtrSizeInBits() / 8);
868 clang::CharUnits eltAlign = clang::CharUnits::fromQuantity(
869 dataLayout.getTypePreferredAlignment(elementType));
870 return std::max(sizeOfSizeT, eltAlign);
871}
872
873mlir::Value LowerItaniumCXXABI::readArrayCookieImpl(
874 mlir::Location loc, mlir::Value allocPtr, clang::CharUnits cookieSize,
875 clang::CharUnits cookieAlignment, const mlir::DataLayout &dataLayout,
876 CIRBaseBuilderTy &builder) const {
877 unsigned ptrSizeInBits = getPtrSizeInBits();
878 auto u8PtrTy = builder.getPointerTo(builder.getUIntNTy(8));
879 auto ptrDiffTy = builder.getSIntNTy(ptrSizeInBits);
880 auto sizeTy = builder.getUIntNTy(ptrSizeInBits);
881
882 // The element count is right-justified in the cookie.
883 clang::CharUnits sizeOfSizeT =
884 clang::CharUnits::fromQuantity(ptrSizeInBits / 8);
885 clang::CharUnits countOffset = cookieSize - sizeOfSizeT;
886
887 mlir::Value countBytePtr = allocPtr;
888 clang::CharUnits countAlignment = cookieAlignment;
889 if (!countOffset.isZero()) {
890 mlir::Value offsetVal = cir::ConstantOp::create(
891 builder, loc, cir::IntAttr::get(ptrDiffTy, countOffset.getQuantity()));
892 countBytePtr =
893 cir::PtrStrideOp::create(builder, loc, u8PtrTy, allocPtr, offsetVal);
894 countAlignment = cookieAlignment.alignmentAtOffset(countOffset);
895 }
896
897 auto countPtrTy = cir::PointerType::get(sizeTy);
898 mlir::Value countPtr = cir::CastOp::create(
899 builder, loc, countPtrTy, cir::CastKind::bitcast, countBytePtr);
900 return cir::LoadOp::create(
901 builder, loc, countPtr, /*isDeref=*/false, /*isVolatile=*/false,
902 builder.getI64IntegerAttr(countAlignment.getQuantity()),
903 cir::SyncScopeKindAttr(), cir::MemOrderAttr());
904}
905
906} // namespace cir
mlir::MLIRContext * getMLIRContext()
Definition LowerModule.h:48
clang::TargetCXXABI::Kind getCXXABIKind() const
Definition LowerModule.h:41
const clang::TargetInfo & getTarget() const
Definition LowerModule.h:47
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset?
Definition CharUnits.h:207
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition CharUnits.h:122
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
Exposes information about the current target.
Definition TargetInfo.h:227
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
Definition TargetInfo.h:408
uint64_t getPointerAlign(LangAS AddrSpace) const
Definition TargetInfo.h:494
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.
long int64_t
static bool addressSpace()
static bool appleArm64CXXABI()
static bool emitCFICheck()
static bool emitVFEInfo()
static bool emitWPDInfo()
static bool emitTypeCheck()
static bool pointerAuthentication()
IntType
===-— Target Data Type Query Methods ----------------------------—===//
Definition TargetInfo.h:147