Path: blob/main/contrib/llvm-project/clang/lib/CIR/CodeGen/Address.h
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//===----------------------------------------------------------------------===//7//8// This class provides a simple wrapper for a pair of a pointer and an9// alignment.10//11//===----------------------------------------------------------------------===//1213#ifndef CLANG_LIB_CIR_ADDRESS_H14#define CLANG_LIB_CIR_ADDRESS_H1516#include "mlir/IR/Value.h"17#include "clang/AST/CharUnits.h"18#include "clang/CIR/Dialect/IR/CIRTypes.h"19#include "llvm/ADT/PointerIntPair.h"2021namespace clang::CIRGen {2223// Forward declaration to avoid a circular dependency24class CIRGenBuilderTy;2526class Address {2728// The boolean flag indicates whether the pointer is known to be non-null.29llvm::PointerIntPair<mlir::Value, 1, bool> pointerAndKnownNonNull;3031/// The expected CIR type of the pointer. Carrying accurate element type32/// information in Address makes it more convenient to work with Address33/// values and allows frontend assertions to catch simple mistakes.34mlir::Type elementType;3536clang::CharUnits alignment;3738protected:39Address(std::nullptr_t) : elementType(nullptr) {}4041public:42Address(mlir::Value pointer, mlir::Type elementType,43clang::CharUnits alignment)44: pointerAndKnownNonNull(pointer, false), elementType(elementType),45alignment(alignment) {46assert(mlir::isa<cir::PointerType>(pointer.getType()) &&47"Expected cir.ptr type");4849assert(pointer && "Pointer cannot be null");50assert(elementType && "Element type cannot be null");51assert(!alignment.isZero() && "Alignment cannot be zero");5253assert(mlir::cast<cir::PointerType>(pointer.getType()).getPointee() ==54elementType);55}5657Address(mlir::Value pointer, clang::CharUnits alignment)58: Address(pointer,59mlir::cast<cir::PointerType>(pointer.getType()).getPointee(),60alignment) {61assert((!alignment.isZero() || pointer == nullptr) &&62"creating valid address with invalid alignment");63}6465static Address invalid() { return Address(nullptr); }66bool isValid() const {67return pointerAndKnownNonNull.getPointer() != nullptr;68}6970/// Return address with different element type, a bitcast pointer, and71/// the same alignment.72Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const;7374mlir::Value getPointer() const {75assert(isValid());76return pointerAndKnownNonNull.getPointer();77}7879mlir::Value getBasePointer() const {80// TODO(cir): Remove the version above when we catchup with OG codegen on81// ptr auth.82assert(isValid() && "pointer isn't valid");83return getPointer();84}8586mlir::Type getType() const {87assert(mlir::cast<cir::PointerType>(88pointerAndKnownNonNull.getPointer().getType())89.getPointee() == elementType);9091return mlir::cast<cir::PointerType>(getPointer().getType());92}9394mlir::Type getElementType() const {95assert(isValid());96assert(mlir::cast<cir::PointerType>(97pointerAndKnownNonNull.getPointer().getType())98.getPointee() == elementType);99return elementType;100}101102clang::CharUnits getAlignment() const { return alignment; }103};104105} // namespace clang::CIRGen106107#endif // CLANG_LIB_CIR_ADDRESS_H108109110