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
11using namespace clang::CIRGen;
12
13mlir::Value CIRGenBuilderTy::maybeBuildArrayDecay(mlir::Location loc,
14 mlir::Value arrayPtr,
15 mlir::Type eltTy) {
16 const auto arrayPtrTy = mlir::cast<cir::PointerType>(arrayPtr.getType());
17 const auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
18
19 if (arrayTy) {
20 const cir::PointerType flatPtrTy = getPointerTo(arrayTy.getElementType());
21 return create<cir::CastOp>(loc, flatPtrTy, cir::CastKind::array_to_ptrdecay,
22 arrayPtr);
23 }
24
25 assert(arrayPtrTy.getPointee() == eltTy &&
26 "flat pointee type must match original array element type");
27 return arrayPtr;
28}
29
30mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin,
31 mlir::Location arrayLocEnd,
32 mlir::Value arrayPtr,
33 mlir::Type eltTy, mlir::Value idx,
34 bool shouldDecay) {
35 mlir::Value basePtr = arrayPtr;
36 if (shouldDecay)
37 basePtr = maybeBuildArrayDecay(arrayLocBegin, arrayPtr, eltTy);
38 const mlir::Type flatPtrTy = basePtr.getType();
39 return create<cir::PtrStrideOp>(arrayLocEnd, flatPtrTy, basePtr, idx);
40}
41
42cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
43 llvm::APSInt intVal) {
44 bool isSigned = intVal.isSigned();
45 unsigned width = intVal.getBitWidth();
46 cir::IntType t = isSigned ? getSIntNTy(width) : getUIntNTy(width);
47 return getConstInt(loc, t,
48 isSigned ? intVal.getSExtValue() : intVal.getZExtValue());
49}
50
51cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
52 llvm::APInt intVal) {
53 return getConstInt(loc, llvm::APSInt(intVal));
54}
55
56cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t,
57 uint64_t c) {
58 assert(mlir::isa<cir::IntType>(t) && "expected cir::IntType");
59 return create<cir::ConstantOp>(loc, cir::IntAttr::get(t, c));
60}
61
62cir::ConstantOp
63clang::CIRGen::CIRGenBuilderTy::getConstFP(mlir::Location loc, mlir::Type t,
64 llvm::APFloat fpVal) {
65 assert(mlir::isa<cir::CIRFPTypeInterface>(t) &&
66 "expected floating point type");
67 return create<cir::ConstantOp>(loc, getAttr<cir::FPAttr>(t, fpVal));
68}
69
70// This can't be defined in Address.h because that file is included by
71// CIRGenBuilder.h
72Address Address::withElementType(CIRGenBuilderTy &builder,
73 mlir::Type elemTy) const {
74 assert(!cir::MissingFeatures::addressOffset());
75 assert(!cir::MissingFeatures::addressIsKnownNonNull());
76 assert(!cir::MissingFeatures::addressPointerAuthInfo());
77
78 return Address(builder.createPtrBitcast(getBasePointer(), elemTy), elemTy,
79 getAlignment());
80}
81

source code of clang/lib/CIR/CodeGen/CIRGenBuilder.cpp