clang 24.0.0git
CIRGenBuilder.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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#include "CIRGenBuilder.h"
10#include "mlir/IR/BuiltinAttributes.h"
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/TypeSwitch.h"
14
15using namespace clang::CIRGen;
16
17mlir::Value CIRGenBuilderTy::maybeBuildArrayDecay(mlir::Location loc,
18 mlir::Value arrayPtr,
19 mlir::Type eltTy) {
20 const auto arrayPtrTy = mlir::cast<cir::PointerType>(arrayPtr.getType());
21 const auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
22
23 if (arrayTy) {
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);
28 }
29
30 assert(arrayPtrTy.getPointee() == eltTy &&
31 "flat pointee type must match original array element type");
32 return arrayPtr;
33}
34
35mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin,
36 mlir::Location arrayLocEnd,
37 mlir::Value arrayPtr,
38 mlir::Type eltTy, mlir::Value idx,
39 bool shouldDecay,
40 cir::PtrOffsetFlags flags) {
41 auto arrayPtrTy = mlir::dyn_cast<cir::PointerType>(arrayPtr.getType());
42 assert(arrayPtrTy && "expected pointer type");
43 // If the array pointer is not decayed, emit a GetElementOp.
44 auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
45
46 assert(mlir::isa<cir::IntType>(idx.getType()) &&
48 mlir::cast<cir::IntType>(idx.getType()).getWidth()));
49
50 if (shouldDecay && arrayTy && arrayTy == eltTy) {
51 auto eltPtrTy =
52 getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
53 return cir::GetElementOp::create(
54 *this, arrayLocEnd, eltPtrTy, arrayPtr, idx,
57 }
58
59 // If we don't have sufficient type information, emit a PtrStrideOp.
60 mlir::Value basePtr = arrayPtr;
61 if (shouldDecay)
62 basePtr = maybeBuildArrayDecay(arrayLocBegin, arrayPtr, eltTy);
63 const mlir::Type flatPtrTy = basePtr.getType();
64 return cir::PtrStrideOp::create(*this, arrayLocEnd, flatPtrTy, basePtr, idx);
65}
66
67cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
68 llvm::APSInt intVal) {
69 bool isSigned = intVal.isSigned();
70 unsigned width = intVal.getBitWidth();
71 cir::IntType t = isSigned ? getSIntNTy(width) : getUIntNTy(width);
72 return getConstInt(loc, t,
73 isSigned ? intVal.getSExtValue() : intVal.getZExtValue());
74}
75
76cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
77 llvm::APInt intVal,
78 bool isUnsigned) {
79 return getConstInt(loc, llvm::APSInt(intVal, isUnsigned));
80}
81
82cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t,
83 uint64_t c) {
84 assert(mlir::isa<cir::IntType>(t) && "expected cir::IntType");
85 return cir::ConstantOp::create(*this, loc, cir::IntAttr::get(t, c));
86}
87
88cir::ConstantOp
89clang::CIRGen::CIRGenBuilderTy::getConstFP(mlir::Location loc, mlir::Type t,
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));
93}
94
96 int64_t offset, mlir::Type ty, const cir::CIRDataLayout &layout,
98 if (!offset)
99 return true;
100
101 // Compute floor-division and a non-negative remainder. A negative flat
102 // offset (e.g. from a pointer one element before the start of an array)
103 // must translate to a negative array index with a non-negative remainder
104 // so that the recursive call can descend into the element type without
105 // a negative offset flowing into the record case below.
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;
110 if (modRet < 0) {
111 divRet -= 1;
112 modRet += eltSize;
113 }
114 return {divRet, modRet};
115 };
116
117 mlir::Type subType =
118 llvm::TypeSwitch<mlir::Type, mlir::Type>(ty)
119 .Case<cir::ArrayType>([&](auto arrayTy) {
120 int64_t eltSize = layout.getTypeAllocSize(arrayTy.getElementType());
121 const auto [index, newOffset] =
122 getIndexAndNewOffset(offset, eltSize);
123 indices.push_back(index);
124 offset = newOffset;
125 return arrayTy.getElementType();
126 })
127 .Case<cir::RecordType>([&](auto recordTy) -> mlir::Type {
128 // A record can only be entered from its start. An offset before
129 // the record, or at or past its end, designates an address outside
130 // of the object, which no member index can reach.
131 if (offset < 0)
132 return {};
133 ArrayRef<mlir::Type> elts = recordTy.getMembers();
134 int64_t pos = 0;
135 for (size_t i = 0; i < elts.size(); ++i) {
136 int64_t eltSize =
137 (int64_t)layout.getTypeAllocSize(elts[i]).getFixedValue();
138 unsigned alignMask = layout.getABITypeAlign(elts[i]).value() - 1;
139 if (recordTy.getPacked())
140 alignMask = 0;
141 // Union's fields have the same offset, so no need to change pos
142 // here, we just need to find eltSize that is greater then the
143 // required offset. The same is true for the similar union type
144 // check below
145 if (!recordTy.isUnion())
146 pos = (pos + alignMask) & ~alignMask;
147 if (offset < pos + eltSize) {
148 indices.push_back(i);
149 offset -= pos;
150 return elts[i];
151 }
152 // No need to update pos here, see the comment above.
153 if (!recordTy.isUnion())
154 pos += eltSize;
155 }
156 return {};
157 })
158 .Case<cir::IntType>([&](cir::IntType intTy) -> mlir::Type {
159 // Integer element type: the offset is a flat element count.
160 // This covers pointer arithmetic through a plain integer base,
161 // e.g. a char* GlobalViewAttr whose pointee type is !s8i rather
162 // than an array — the GEP is getelementptr i8, ptr @sym, i64 N.
163 int64_t eltSize =
164 (int64_t)layout.getTypeAllocSize(intTy).getFixedValue();
165 assert(eltSize > 0 && "element size must be positive");
166 const auto [index, newOffset] =
167 getIndexAndNewOffset(offset, eltSize);
168 // An integer has no subelements, so an offset into the middle of
169 // one can't be indexed.
170 if (newOffset)
171 return {};
172 indices.push_back(index);
173 offset = newOffset;
174 return intTy;
175 })
176 .Default([](mlir::Type) -> mlir::Type { return {}; });
177
178 if (!subType)
179 return false;
180
181 return computeGlobalViewIndicesFromFlatOffset(offset, subType, layout,
182 indices);
183}
184
186 const cir::CIRDataLayout &layout, mlir::Type ty,
187 llvm::ArrayRef<int64_t> indices) {
188 int64_t offset = 0;
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();
196 offset += layout.getTypeAllocSize(ty) * idx;
197 } else if (mlir::isa<cir::IntType>(ty)) {
198 // Integer element type: the index is a flat element count.
199 offset += (int64_t)layout.getTypeAllocSize(ty).getFixedValue() * idx;
200 } else {
201 llvm_unreachable("unexpected type");
202 }
203 }
204 return offset;
205}
206
208 mlir::ArrayAttr arrayAttr, cir::RecordType recordTy) {
209 // Return zero or anonymous constant record.
210 const bool isZero = llvm::all_of(
211 arrayAttr, [&](mlir::Attribute a) { return isNullValue(a); });
212 if (isZero)
213 return cir::ZeroAttr::get(recordTy);
214 return cir::ConstRecordAttr::get(recordTy, arrayAttr);
215}
216
217// This can't be defined in Address.h because that file is included by
218// CIRGenBuilder.h
220 mlir::Type elemTy) const {
224
225 return Address(builder.createPtrBitcast(getBasePointer(), elemTy), elemTy,
226 getAlignment());
227}
static bool isUnsigned(SValBuilder &SVB, NonLoc Value)
cir::PointerType getPointerTo(mlir::Type ty)
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
mlir::DataLayout layout
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.
Definition CIRTypes.h:153
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
Definition Address.h:138
mlir::Value getBasePointer() const
Definition Address.h:103
Address(std::nullptr_t)
Definition Address.h:46
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()