clang 23.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 const mlir::DataLayout &dataLayout,
27 CIRBaseBuilderTy &builder,
28 mlir::Value &numElements, mlir::Value &allocPtr,
29 clang::CharUnits &cookieSize) const {
30 auto u8PtrTy = builder.getPointerTo(builder.getUIntNTy(8));
31 auto ptrDiffTy = builder.getSIntNTy(getPtrSizeInBits());
32 auto voidPtrTy = builder.getVoidPtrTy();
33
34 auto ptrTy = mlir::cast<cir::PointerType>(elementPtr.getType());
35 cookieSize = getArrayCookieSizeImpl(ptrTy.getPointee(), dataLayout);
36
37 mlir::Value bytePtr = cir::CastOp::create(builder, loc, u8PtrTy,
38 cir::CastKind::bitcast, elementPtr);
39
40 mlir::Value negCookieSize = cir::ConstantOp::create(
41 builder, loc, cir::IntAttr::get(ptrDiffTy, -cookieSize.getQuantity()));
42 mlir::Value allocBytePtr =
43 cir::PtrStrideOp::create(builder, loc, u8PtrTy, bytePtr, negCookieSize);
44
45 allocPtr = cir::CastOp::create(builder, loc, voidPtrTy,
46 cir::CastKind::bitcast, allocBytePtr);
47
48 // cookieSize is always a multiple of the element ABI alignment (both are
49 // powers of 2 and cookieSize >= elementAlign), so subtracting it preserves
50 // alignment. The cookie alignment therefore equals the element alignment.
52 dataLayout.getTypePreferredAlignment(ptrTy.getPointee()));
53 numElements = readArrayCookieImpl(loc, allocBytePtr, cookieSize,
54 cookieAlignment, 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 clang::CharUnits getArrayCookieSizeImpl(mlir::Type elementType, const mlir::DataLayout &dataLayout) const =0
Returns the cookie size in bytes for a dynamically-allocated array of elements with the given type.
virtual ~CIRCXXABI()
Definition CIRCXXABI.cpp:19
void readArrayCookie(mlir::Location loc, mlir::Value elementPtr, 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
LowerModule & lm
Definition CIRCXXABI.h:32
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
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
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