| 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 | // This class provides a simple wrapper for a pair of a pointer and an |
| 10 | // alignment. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef CLANG_LIB_CIR_ADDRESS_H |
| 15 | #define CLANG_LIB_CIR_ADDRESS_H |
| 16 | |
| 17 | #include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h" |
| 18 | #include "mlir/IR/Value.h" |
| 19 | #include "clang/AST/CharUnits.h" |
| 20 | #include "clang/CIR/Dialect/IR/CIRAttrs.h" |
| 21 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 22 | #include "clang/CIR/Dialect/IR/CIROpsEnums.h" |
| 23 | #include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 24 | #include "clang/CIR/MissingFeatures.h" |
| 25 | #include "llvm/ADT/PointerIntPair.h" |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | |
| 28 | namespace clang::CIRGen { |
| 29 | |
| 30 | // Forward declaration to avoid a circular dependency |
| 31 | class CIRGenBuilderTy; |
| 32 | |
| 33 | class Address { |
| 34 | |
| 35 | // The boolean flag indicates whether the pointer is known to be non-null. |
| 36 | llvm::PointerIntPair<mlir::Value, 1, bool> pointerAndKnownNonNull; |
| 37 | |
| 38 | /// The expected CIR type of the pointer. Carrying accurate element type |
| 39 | /// information in Address makes it more convenient to work with Address |
| 40 | /// values and allows frontend assertions to catch simple mistakes. |
| 41 | mlir::Type elementType; |
| 42 | |
| 43 | clang::CharUnits alignment; |
| 44 | |
| 45 | protected: |
| 46 | Address(std::nullptr_t) : elementType(nullptr) {} |
| 47 | |
| 48 | public: |
| 49 | Address(mlir::Value pointer, mlir::Type elementType, |
| 50 | clang::CharUnits alignment) |
| 51 | : Address(pointer, elementType, alignment, false) {} |
| 52 | |
| 53 | Address(mlir::Value pointer, mlir::Type elementType, |
| 54 | clang::CharUnits alignment, bool isKnownNonNull) |
| 55 | : pointerAndKnownNonNull(pointer, isKnownNonNull), |
| 56 | elementType(elementType), alignment(alignment) { |
| 57 | assert(pointer && "Pointer cannot be null" ); |
| 58 | assert(elementType && "Element type cannot be null" ); |
| 59 | assert(!alignment.isZero() && "Alignment cannot be zero" ); |
| 60 | |
| 61 | assert(mlir::isa<cir::PointerType>(pointer.getType()) && |
| 62 | "Expected cir.ptr type" ); |
| 63 | |
| 64 | assert(mlir::cast<cir::PointerType>(pointer.getType()).getPointee() == |
| 65 | elementType); |
| 66 | } |
| 67 | |
| 68 | Address(mlir::Value pointer, clang::CharUnits alignment) |
| 69 | : Address(pointer, |
| 70 | mlir::cast<cir::PointerType>(Val: pointer.getType()).getPointee(), |
| 71 | alignment) { |
| 72 | assert((!alignment.isZero() || pointer == nullptr) && |
| 73 | "creating valid address with invalid alignment" ); |
| 74 | } |
| 75 | |
| 76 | static Address invalid() { return Address(nullptr); } |
| 77 | bool isValid() const { |
| 78 | return pointerAndKnownNonNull.getPointer() != nullptr; |
| 79 | } |
| 80 | |
| 81 | /// Return address with different pointer, but same element type and |
| 82 | /// alignment. |
| 83 | Address withPointer(mlir::Value newPtr) const { |
| 84 | return Address(newPtr, getElementType(), getAlignment()); |
| 85 | } |
| 86 | |
| 87 | /// Return address with different alignment, but same pointer and element |
| 88 | /// type. |
| 89 | Address withAlignment(clang::CharUnits newAlignment) const { |
| 90 | return Address(getPointer(), getElementType(), newAlignment, |
| 91 | isKnownNonNull()); |
| 92 | } |
| 93 | |
| 94 | /// Return address with different element type, a bitcast pointer, and |
| 95 | /// the same alignment. |
| 96 | Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const; |
| 97 | |
| 98 | mlir::Value getPointer() const { |
| 99 | assert(isValid()); |
| 100 | return pointerAndKnownNonNull.getPointer(); |
| 101 | } |
| 102 | |
| 103 | mlir::Value getBasePointer() const { |
| 104 | // TODO(cir): Remove the version above when we catchup with OG codegen on |
| 105 | // ptr auth. |
| 106 | assert(isValid() && "pointer isn't valid" ); |
| 107 | return getPointer(); |
| 108 | } |
| 109 | |
| 110 | /// Return the pointer contained in this class after authenticating it and |
| 111 | /// adding offset to it if necessary. |
| 112 | mlir::Value emitRawPointer() const { |
| 113 | assert(!cir::MissingFeatures::addressPointerAuthInfo()); |
| 114 | return getBasePointer(); |
| 115 | } |
| 116 | |
| 117 | mlir::Type getType() const { |
| 118 | assert(mlir::cast<cir::PointerType>( |
| 119 | pointerAndKnownNonNull.getPointer().getType()) |
| 120 | .getPointee() == elementType); |
| 121 | |
| 122 | return mlir::cast<cir::PointerType>(Val: getPointer().getType()); |
| 123 | } |
| 124 | |
| 125 | mlir::Type getElementType() const { |
| 126 | assert(isValid()); |
| 127 | assert(mlir::cast<cir::PointerType>( |
| 128 | pointerAndKnownNonNull.getPointer().getType()) |
| 129 | .getPointee() == elementType); |
| 130 | return elementType; |
| 131 | } |
| 132 | |
| 133 | mlir::ptr::MemorySpaceAttrInterface getAddressSpace() const { |
| 134 | auto ptrTy = mlir::dyn_cast<cir::PointerType>(Val: getType()); |
| 135 | return ptrTy.getAddrSpace(); |
| 136 | } |
| 137 | |
| 138 | clang::CharUnits getAlignment() const { return alignment; } |
| 139 | |
| 140 | /// Get the operation which defines this address. |
| 141 | mlir::Operation *getDefiningOp() const { |
| 142 | if (!isValid()) |
| 143 | return nullptr; |
| 144 | return getPointer().getDefiningOp(); |
| 145 | } |
| 146 | |
| 147 | template <typename OpTy> OpTy getDefiningOp() const { |
| 148 | return mlir::dyn_cast_or_null<OpTy>(getDefiningOp()); |
| 149 | } |
| 150 | |
| 151 | /// Return the underlying alloca for this address, if any. |
| 152 | cir::AllocaOp getUnderlyingAllocaOp() const { |
| 153 | return cir::getUnderlyingAlloca(addr: getPointer()); |
| 154 | } |
| 155 | |
| 156 | /// Whether the pointer is known not to be null. |
| 157 | bool isKnownNonNull() const { |
| 158 | assert(isValid() && "Invalid address" ); |
| 159 | return static_cast<bool>(pointerAndKnownNonNull.getInt()); |
| 160 | } |
| 161 | |
| 162 | /// Set the non-null bit. |
| 163 | Address setKnownNonNull() { |
| 164 | assert(isValid() && "Invalid address" ); |
| 165 | pointerAndKnownNonNull.setInt(true); |
| 166 | return *this; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | } // namespace clang::CIRGen |
| 171 | |
| 172 | #endif // CLANG_LIB_CIR_ADDRESS_H |
| 173 | |