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"
11#include "clang/CIR/MissingFeatures.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>(Val: arrayPtr.getType());
21 const auto arrayTy = mlir::dyn_cast<cir::ArrayType>(Val: arrayPtrTy.getPointee());
22
23 if (arrayTy) {
24 const cir::PointerType flatPtrTy =
25 getPointerTo(ty: arrayTy.getElementType(), as: arrayPtrTy.getAddrSpace());
26 return cir::CastOp::create(builder&: *this, location: loc, result: flatPtrTy,
27 kind: cir::CastKind::array_to_ptrdecay, src: 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>(Val: 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>(Val: arrayPtrTy.getPointee());
45
46 assert(mlir::isa<cir::IntType>(idx.getType()) &&
47 cir::isValidFundamentalIntWidth(
48 mlir::cast<cir::IntType>(idx.getType()).getWidth()));
49
50 if (shouldDecay && arrayTy && arrayTy == eltTy) {
51 auto eltPtrTy =
52 getPointerTo(ty: arrayTy.getElementType(), as: arrayPtrTy.getAddrSpace());
53 return cir::GetElementOp::create(
54 builder&: *this, location: arrayLocEnd, result: eltPtrTy, base: arrayPtr, index: idx,
55 in_bounds: any(Val: flags & cir::PtrOffsetFlags::InBounds),
56 no_unsigned_wrap: any(Val: flags & cir::PtrOffsetFlags::NoUnsignedWrap));
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(loc: arrayLocBegin, arrayPtr, eltTy);
63 const mlir::Type flatPtrTy = basePtr.getType();
64 return cir::PtrStrideOp::create(builder&: *this, location: arrayLocEnd, result: flatPtrTy, base: basePtr, stride: 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(n: width) : getUIntNTy(n: width);
72 return getConstInt(loc, t,
73 c: 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, intVal: 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(builder&: *this, location: loc, value: cir::IntAttr::get(type: t, value: 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(builder&: *this, location: loc, value: cir::FPAttr::get(type: t, value: fpVal));
93}
94
95bool CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset(
96 int64_t offset, mlir::Type ty, const cir::CIRDataLayout &layout,
97 llvm::SmallVectorImpl<int64_t> &indices) {
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>(caseFn: [&](auto arrayTy) {
120 int64_t eltSize = layout.getTypeAllocSize(ty: arrayTy.getElementType());
121 const auto [index, newOffset] =
122 getIndexAndNewOffset(offset, eltSize);
123 indices.push_back(Elt: index);
124 offset = newOffset;
125 return arrayTy.getElementType();
126 })
127 .Case<cir::RecordType>(caseFn: [&](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(ty: elts[i]).getFixedValue();
138 unsigned alignMask = layout.getABITypeAlign(ty: 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(Elt: 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>(caseFn: [&](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(ty: 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(Elt: index);
173 offset = newOffset;
174 return intTy;
175 })
176 .Default(defaultFn: [](mlir::Type) -> mlir::Type { return {}; });
177
178 if (!subType)
179 return false;
180
181 return computeGlobalViewIndicesFromFlatOffset(offset, ty: subType, layout,
182 indices);
183}
184
185uint64_t CIRGenBuilderTy::computeOffsetFromGlobalViewIndices(
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>(Val&: ty)) {
191 offset += recordTy.getElementOffset(dataLayout: 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>(Val&: ty)) {
195 ty = arrayTy.getElementType();
196 offset += layout.getTypeAllocSize(ty) * idx;
197 } else if (mlir::isa<cir::IntType>(Val: 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
207mlir::Attribute clang::CIRGen::CIRGenBuilderTy::getConstRecordOrZeroAttr(
208 mlir::ArrayAttr arrayAttr, cir::RecordType recordTy) {
209 // Return zero or anonymous constant record.
210 const bool isZero = llvm::all_of(
211 Range&: arrayAttr, P: [&](mlir::Attribute a) { return isNullValue(attr: a); });
212 if (isZero)
213 return cir::ZeroAttr::get(type: recordTy);
214 return cir::ConstRecordAttr::get(type: recordTy, members: arrayAttr);
215}
216
217// This can't be defined in Address.h because that file is included by
218// CIRGenBuilder.h
219Address Address::withElementType(CIRGenBuilderTy &builder,
220 mlir::Type elemTy) const {
221 assert(!cir::MissingFeatures::addressOffset());
222 assert(!cir::MissingFeatures::addressIsKnownNonNull());
223 assert(!cir::MissingFeatures::addressPointerAuthInfo());
224
225 return Address(builder.createPtrBitcast(src: getBasePointer(), newPointeeTy: elemTy), elemTy,
226 getAlignment());
227}
228