10#include "mlir/IR/BuiltinAttributes.h"
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/TypeSwitch.h"
20 const auto arrayPtrTy = mlir::cast<cir::PointerType>(arrayPtr.getType());
21 const auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
24 const cir::PointerType flatPtrTy =
25 getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
26 return cir::CastOp::create(*
this, loc, flatPtrTy,
27 cir::CastKind::array_to_ptrdecay, arrayPtr);
30 assert(arrayPtrTy.getPointee() == eltTy &&
31 "flat pointee type must match original array element type");
36 mlir::Location arrayLocEnd,
38 mlir::Type eltTy, mlir::Value idx,
41 auto arrayPtrTy = mlir::dyn_cast<cir::PointerType>(arrayPtr.getType());
42 assert(arrayPtrTy &&
"expected pointer type");
44 auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
46 assert(mlir::isa<cir::IntType>(idx.getType()) &&
48 mlir::cast<cir::IntType>(idx.getType()).getWidth()));
50 if (shouldDecay && arrayTy && arrayTy == eltTy) {
52 getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
53 return cir::GetElementOp::create(
54 *
this, arrayLocEnd, eltPtrTy, arrayPtr, idx,
60 mlir::Value basePtr = arrayPtr;
63 const mlir::Type flatPtrTy = basePtr.getType();
64 return cir::PtrStrideOp::create(*
this, arrayLocEnd, flatPtrTy, basePtr, idx);
68 llvm::APSInt intVal) {
69 bool isSigned = intVal.isSigned();
70 unsigned width = intVal.getBitWidth();
73 isSigned ? intVal.getSExtValue() : intVal.getZExtValue());
84 assert(mlir::isa<cir::IntType>(t) &&
"expected cir::IntType");
85 return cir::ConstantOp::create(*
this, loc, cir::IntAttr::get(t, c));
90 llvm::APFloat fpVal) {
91 assert(mlir::isa<cir::FPTypeInterface>(t) &&
"expected floating point type");
92 return cir::ConstantOp::create(*
this, loc, cir::FPAttr::get(t, fpVal));
106 auto getIndexAndNewOffset =
107 [](int64_t offset, int64_t eltSize) -> std::pair<int64_t, int64_t> {
108 int64_t divRet = offset / eltSize;
109 int64_t modRet = offset % eltSize;
114 return {divRet, modRet};
118 llvm::TypeSwitch<mlir::Type, mlir::Type>(ty)
119 .Case<cir::ArrayType>([&](
auto arrayTy) {
121 const auto [
index, newOffset] =
122 getIndexAndNewOffset(offset, eltSize);
123 indices.push_back(
index);
125 return arrayTy.getElementType();
127 .Case<cir::RecordType>([&](
auto recordTy) -> mlir::Type {
135 for (
size_t i = 0; i < elts.size(); ++i) {
139 if (recordTy.getPacked())
145 if (!recordTy.isUnion())
146 pos = (pos + alignMask) & ~alignMask;
147 if (offset < pos + eltSize) {
148 indices.push_back(i);
153 if (!recordTy.isUnion())
158 .Case<cir::IntType>([&](cir::IntType intTy) -> mlir::Type {
165 assert(eltSize > 0 &&
"element size must be positive");
166 const auto [
index, newOffset] =
167 getIndexAndNewOffset(offset, eltSize);
172 indices.push_back(
index);
176 .
Default([](mlir::Type) -> mlir::Type {
return {}; });
189 for (int64_t idx : indices) {
190 if (
auto recordTy = dyn_cast<cir::RecordType>(ty)) {
191 offset += recordTy.getElementOffset(layout.
layout, idx);
192 assert(idx < (int64_t)recordTy.getMembers().size());
193 ty = recordTy.getMembers()[idx];
194 }
else if (
auto arrayTy = dyn_cast<cir::ArrayType>(ty)) {
195 ty = arrayTy.getElementType();
197 }
else if (mlir::isa<cir::IntType>(ty)) {
201 llvm_unreachable(
"unexpected type");
210 const bool isZero = llvm::all_of(
211 arrayAttr, [&](mlir::Attribute a) {
return isNullValue(a); });
213 return cir::ZeroAttr::get(recordTy);
214 return cir::ConstRecordAttr::get(recordTy, arrayAttr);
220 mlir::Type elemTy)
const {
static bool isUnsigned(SValBuilder &SVB, NonLoc Value)
cir::PointerType getPointerTo(mlir::Type ty)
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
llvm::TypeSize getTypeAllocSize(mlir::Type ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
llvm::Align getABITypeAlign(mlir::Type ty) const
C++ view class that accepts both !cir.struct and !cir.union types.
Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const
Return address with different element type, a bitcast pointer, and the same alignment.
clang::CharUnits getAlignment() const
mlir::Value getBasePointer() const
bool computeGlobalViewIndicesFromFlatOffset(int64_t offset, mlir::Type ty, const cir::CIRDataLayout &layout, llvm::SmallVectorImpl< int64_t > &indices)
cir::IntType getSIntNTy(int n)
mlir::Attribute getConstRecordOrZeroAttr(mlir::ArrayAttr arrayAttr, cir::RecordType recordTy)
mlir::Value maybeBuildArrayDecay(mlir::Location loc, mlir::Value arrayPtr, mlir::Type eltTy)
Returns a decayed pointer to the first element of the array pointed to by arrayPtr.
cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t, llvm::APFloat fpVal)
uint64_t computeOffsetFromGlobalViewIndices(const cir::CIRDataLayout &layout, mlir::Type ty, llvm::ArrayRef< int64_t > indices)
bool isNullValue(mlir::Attribute attr) const
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
mlir::Value getArrayElement(mlir::Location arrayLocBegin, mlir::Location arrayLocEnd, mlir::Value arrayPtr, mlir::Type eltTy, mlir::Value idx, bool shouldDecay, cir::PtrOffsetFlags flags=cir::PtrOffsetFlags::None)
Create a cir.get_element or cir.ptr_stride operation to get access to an array element.
cir::IntType getUIntNTy(int n)
bool isValidFundamentalIntWidth(unsigned width)
PtrOffsetFlags
No-wrap flags of a pointer offset computation: the subset of llvm::GEPNoWrapFlags that cir....
@ Default
Set to the current date and time.
int __ovld __cnfn any(char)
Returns 1 if the most significant bit in any component of x is set; otherwise returns 0.
static bool addressPointerAuthInfo()
static bool addressOffset()
static bool addressIsKnownNonNull()