clang 24.0.0git
CIRCXXABI.cpp
Go to the documentation of this file.
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
20
22 return lm.getTarget().getPointerWidth(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>(elementPtr.getType()).getAddrSpace();
33 auto u8PtrTy = builder.getPointerTo(builder.getUIntNTy(8), addrSpace);
34 auto ptrDiffTy = builder.getSIntNTy(getPtrSizeInBits());
35 auto voidPtrTy = builder.getVoidPtrTy(addrSpace);
36
37 cookieSize = getArrayCookieSizeImpl(elementAlign);
38
39 mlir::Value bytePtr = cir::CastOp::create(builder, loc, u8PtrTy,
40 cir::CastKind::bitcast, elementPtr);
41
42 mlir::Value negCookieSize = cir::ConstantOp::create(
43 builder, loc, cir::IntAttr::get(ptrDiffTy, -cookieSize.getQuantity()));
44 mlir::Value allocBytePtr =
45 cir::PtrStrideOp::create(builder, loc, u8PtrTy, bytePtr, negCookieSize);
46
47 allocPtr = cir::CastOp::create(builder, loc, voidPtrTy,
48 cir::CastKind::bitcast, 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, allocBytePtr, cookieSize, elementAlign,
54 dataLayout, builder);
55}
56
57} // namespace cir
cir::PointerType getPointerTo(mlir::Type ty)
cir::IntType getUIntNTy(int n)
cir::PointerType getVoidPtrTy(clang::LangAS langAS=clang::LangAS::Default)
cir::IntType getSIntNTy(int n)
virtual ~CIRCXXABI()
Definition CIRCXXABI.cpp:19
LowerModule & lm
Definition CIRCXXABI.h:32
virtual clang::CharUnits getArrayCookieSizeImpl(clang::CharUnits elementAlign) const =0
Returns the cookie size in bytes for a dynamically-allocated array of elements with the given preferr...
virtual mlir::Value readArrayCookieImpl(mlir::Location loc, mlir::Value allocPtr, clang::CharUnits cookieSize, clang::CharUnits cookieAlignment, const mlir::DataLayout &dataLayout, CIRBaseBuilderTy &builder) const =0
Reads the element count from an array cookie.
unsigned getPtrSizeInBits() const
Definition CIRCXXABI.cpp:21
void readArrayCookie(mlir::Location loc, mlir::Value elementPtr, clang::CharUnits elementAlign, const mlir::DataLayout &dataLayout, CIRBaseBuilderTy &builder, mlir::Value &numElements, mlir::Value &allocPtr, clang::CharUnits &cookieSize) const
Read the array cookie for a dynamically-allocated array whose first element is at elementPtr.
Definition CIRCXXABI.cpp:25
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
Definition CharUnits.h:155