Path: blob/main/contrib/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp
213799 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "CIRGenBuilder.h"910using namespace clang::CIRGen;1112mlir::Value CIRGenBuilderTy::maybeBuildArrayDecay(mlir::Location loc,13mlir::Value arrayPtr,14mlir::Type eltTy) {15const auto arrayPtrTy = mlir::cast<cir::PointerType>(arrayPtr.getType());16const auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());1718if (arrayTy) {19const cir::PointerType flatPtrTy = getPointerTo(arrayTy.getElementType());20return create<cir::CastOp>(loc, flatPtrTy, cir::CastKind::array_to_ptrdecay,21arrayPtr);22}2324assert(arrayPtrTy.getPointee() == eltTy &&25"flat pointee type must match original array element type");26return arrayPtr;27}2829mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin,30mlir::Location arrayLocEnd,31mlir::Value arrayPtr,32mlir::Type eltTy, mlir::Value idx,33bool shouldDecay) {34mlir::Value basePtr = arrayPtr;35if (shouldDecay)36basePtr = maybeBuildArrayDecay(arrayLocBegin, arrayPtr, eltTy);37const mlir::Type flatPtrTy = basePtr.getType();38return create<cir::PtrStrideOp>(arrayLocEnd, flatPtrTy, basePtr, idx);39}4041cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,42llvm::APSInt intVal) {43bool isSigned = intVal.isSigned();44unsigned width = intVal.getBitWidth();45cir::IntType t = isSigned ? getSIntNTy(width) : getUIntNTy(width);46return getConstInt(loc, t,47isSigned ? intVal.getSExtValue() : intVal.getZExtValue());48}4950cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,51llvm::APInt intVal) {52return getConstInt(loc, llvm::APSInt(intVal));53}5455cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t,56uint64_t c) {57assert(mlir::isa<cir::IntType>(t) && "expected cir::IntType");58return create<cir::ConstantOp>(loc, cir::IntAttr::get(t, c));59}6061cir::ConstantOp62clang::CIRGen::CIRGenBuilderTy::getConstFP(mlir::Location loc, mlir::Type t,63llvm::APFloat fpVal) {64assert(mlir::isa<cir::FPTypeInterface>(t) && "expected floating point type");65return create<cir::ConstantOp>(loc, cir::FPAttr::get(t, fpVal));66}6768// This can't be defined in Address.h because that file is included by69// CIRGenBuilder.h70Address Address::withElementType(CIRGenBuilderTy &builder,71mlir::Type elemTy) const {72assert(!cir::MissingFeatures::addressOffset());73assert(!cir::MissingFeatures::addressIsKnownNonNull());74assert(!cir::MissingFeatures::addressPointerAuthInfo());7576return Address(builder.createPtrBitcast(getBasePointer(), elemTy), elemTy,77getAlignment());78}798081