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 11 using namespace clang::CIRGen; 12 13 mlir::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 30 mlir::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 42 cir::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 51 cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, 52 llvm::APInt intVal) { 53 return getConstInt(loc, llvm::APSInt(intVal)); 54 } 55 56 cir::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 62 cir::ConstantOp 63 clang::CIRGen::CIRGenBuilderTy::getConstFP(mlir::Location loc, mlir::Type t, 64 llvm::APFloat fpVal) { 65 assert(mlir::isa<cir::FPTypeInterface>(t) && "expected floating point type"); 66 return create<cir::ConstantOp>(loc, cir::FPAttr::get(t, fpVal)); 67 } 68 69 // This can't be defined in Address.h because that file is included by 70 // CIRGenBuilder.h 71 Address Address::withElementType(CIRGenBuilderTy &builder, 72 mlir::Type elemTy) const { 73 assert(!cir::MissingFeatures::addressOffset()); 74 assert(!cir::MissingFeatures::addressIsKnownNonNull()); 75 assert(!cir::MissingFeatures::addressPointerAuthInfo()); 76 77 return Address(builder.createPtrBitcast(getBasePointer(), elemTy), elemTy, 78 getAlignment()); 79 } 80