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 auto u8PtrTy = builder.getPointerTo(builder.getUIntNTy(8));
32 auto ptrDiffTy = builder.getSIntNTy(getPtrSizeInBits());
33 auto voidPtrTy = builder.getVoidPtrTy();
34
35 cookieSize = getArrayCookieSizeImpl(elementAlign);
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 alignment (both are
49 // powers of 2 and cookieSize >= elementAlign), so subtracting it preserves
50 // alignment. The cookie alignment therefore equals the element alignment.
51 numElements = readArrayCookieImpl(loc, allocBytePtr, cookieSize, elementAlign,
52 dataLayout, builder);
53}
54
55} // 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:153