1//===- CIRCXXABI.cpp ------------------------------------------------------===//
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 clang/lib/CodeGen/CGCXXABI.cpp. The queries are
10// adapted to operate on the CIR dialect, however.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CIRCXXABI.h"
15#include "LowerModule.h"
16
17namespace cir {
18
19CIRCXXABI::~CIRCXXABI() {}
20
21unsigned CIRCXXABI::getPtrSizeInBits() const {
22 return lm.getTarget().getPointerWidth(AddrSpace: clang::LangAS::Default);
23}
24
25void CIRCXXABI::readArrayCookie(mlir::Location loc, mlir::Value elementPtr,
26 clang::CharUnits elementAlign,
27 const mlir::DataLayout &dataLayout,
28 CIRBaseBuilderTy &builder,
29 mlir::Value &numElements, mlir::Value &allocPtr,
30 clang::CharUnits &cookieSize) const {
31 mlir::ptr::MemorySpaceAttrInterface addrSpace =
32 mlir::cast<cir::PointerType>(Val: elementPtr.getType()).getAddrSpace();
33 auto u8PtrTy = builder.getPointerTo(ty: builder.getUIntNTy(n: 8), as: addrSpace);
34 auto ptrDiffTy = builder.getSIntNTy(n: getPtrSizeInBits());
35 auto voidPtrTy = builder.getVoidPtrTy(as: addrSpace);
36
37 cookieSize = getArrayCookieSizeImpl(elementAlign);
38
39 mlir::Value bytePtr = cir::CastOp::create(builder, location: loc, result: u8PtrTy,
40 kind: cir::CastKind::bitcast, src: elementPtr);
41
42 mlir::Value negCookieSize = cir::ConstantOp::create(
43 builder, location: loc, value: cir::IntAttr::get(type: ptrDiffTy, value: -cookieSize.getQuantity()));
44 mlir::Value allocBytePtr =
45 cir::PtrStrideOp::create(builder, location: loc, result: u8PtrTy, base: bytePtr, stride: negCookieSize);
46
47 allocPtr = cir::CastOp::create(builder, location: loc, result: voidPtrTy,
48 kind: cir::CastKind::bitcast, src: allocBytePtr);
49
50 // cookieSize is always a multiple of the element alignment (both are
51 // powers of 2 and cookieSize >= elementAlign), so subtracting it preserves
52 // alignment. The cookie alignment therefore equals the element alignment.
53 numElements = readArrayCookieImpl(loc, allocPtr: allocBytePtr, cookieSize, cookieAlignment: elementAlign,
54 dataLayout, builder);
55}
56
57} // namespace cir
58