clang 22.0.0git
Address.h
Go to the documentation of this file.
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/IR/Value.h"
18#include "clang/AST/CharUnits.h"
22#include "llvm/ADT/PointerIntPair.h"
23#include "llvm/Support/Casting.h"
24
25namespace clang::CIRGen {
26
27// Forward declaration to avoid a circular dependency
28class CIRGenBuilderTy;
29
30class Address {
31
32 // The boolean flag indicates whether the pointer is known to be non-null.
33 llvm::PointerIntPair<mlir::Value, 1, bool> pointerAndKnownNonNull;
34
35 /// The expected CIR type of the pointer. Carrying accurate element type
36 /// information in Address makes it more convenient to work with Address
37 /// values and allows frontend assertions to catch simple mistakes.
38 mlir::Type elementType;
39
40 clang::CharUnits alignment;
41
42protected:
43 Address(std::nullptr_t) : elementType(nullptr) {}
44
45public:
46 Address(mlir::Value pointer, mlir::Type elementType,
47 clang::CharUnits alignment)
48 : pointerAndKnownNonNull(pointer, false), elementType(elementType),
49 alignment(alignment) {
50 assert(pointer && "Pointer cannot be null");
51 assert(elementType && "Element type cannot be null");
52 assert(!alignment.isZero() && "Alignment cannot be zero");
53
54 assert(mlir::isa<cir::PointerType>(pointer.getType()) &&
55 "Expected cir.ptr type");
56
57 assert(mlir::cast<cir::PointerType>(pointer.getType()).getPointee() ==
58 elementType);
59 }
60
61 Address(mlir::Value pointer, clang::CharUnits alignment)
62 : Address(pointer,
63 mlir::cast<cir::PointerType>(pointer.getType()).getPointee(),
64 alignment) {
65 assert((!alignment.isZero() || pointer == nullptr) &&
66 "creating valid address with invalid alignment");
67 }
68
69 static Address invalid() { return Address(nullptr); }
70 bool isValid() const {
71 return pointerAndKnownNonNull.getPointer() != nullptr;
72 }
73
74 /// Return address with different pointer, but same element type and
75 /// alignment.
76 Address withPointer(mlir::Value newPtr) const {
77 return Address(newPtr, getElementType(), getAlignment());
78 }
79
80 /// Return address with different element type, a bitcast pointer, and
81 /// the same alignment.
82 Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const;
83
84 mlir::Value getPointer() const {
85 assert(isValid());
86 return pointerAndKnownNonNull.getPointer();
87 }
88
89 mlir::Value getBasePointer() const {
90 // TODO(cir): Remove the version above when we catchup with OG codegen on
91 // ptr auth.
92 assert(isValid() && "pointer isn't valid");
93 return getPointer();
94 }
95
96 /// Return the pointer contained in this class after authenticating it and
97 /// adding offset to it if necessary.
98 mlir::Value emitRawPointer() const {
100 return getBasePointer();
101 }
102
103 mlir::Type getType() const {
104 assert(mlir::cast<cir::PointerType>(
105 pointerAndKnownNonNull.getPointer().getType())
106 .getPointee() == elementType);
107
108 return mlir::cast<cir::PointerType>(getPointer().getType());
109 }
110
111 mlir::Type getElementType() const {
112 assert(isValid());
113 assert(mlir::cast<cir::PointerType>(
114 pointerAndKnownNonNull.getPointer().getType())
115 .getPointee() == elementType);
116 return elementType;
117 }
118
119 cir::TargetAddressSpaceAttr getAddressSpace() const {
120 auto ptrTy = mlir::dyn_cast<cir::PointerType>(getType());
121 return ptrTy.getAddrSpace();
122 }
123
124 clang::CharUnits getAlignment() const { return alignment; }
125
126 /// Get the operation which defines this address.
127 mlir::Operation *getDefiningOp() const {
128 if (!isValid())
129 return nullptr;
130 return getPointer().getDefiningOp();
131 }
132
133 template <typename OpTy> OpTy getDefiningOp() const {
134 return mlir::dyn_cast_or_null<OpTy>(getDefiningOp());
135 }
136};
137
138} // namespace clang::CIRGen
139
140#endif // CLANG_LIB_CIR_ADDRESS_H
Address withPointer(mlir::Value newPtr) const
Return address with different pointer, but same element type and alignment.
Definition Address.h:76
mlir::Value getPointer() const
Definition Address.h:84
Address(mlir::Value pointer, clang::CharUnits alignment)
Definition Address.h:61
mlir::Type getElementType() const
Definition Address.h:111
static Address invalid()
Definition Address.h:69
Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const
Return address with different element type, a bitcast pointer, and the same alignment.
cir::TargetAddressSpaceAttr getAddressSpace() const
Definition Address.h:119
clang::CharUnits getAlignment() const
Definition Address.h:124
mlir::Value getBasePointer() const
Definition Address.h:89
mlir::Type getType() const
Definition Address.h:103
Address(std::nullptr_t)
Definition Address.h:43
bool isValid() const
Definition Address.h:70
Address(mlir::Value pointer, mlir::Type elementType, clang::CharUnits alignment)
Definition Address.h:46
OpTy getDefiningOp() const
Definition Address.h:133
mlir::Operation * getDefiningOp() const
Get the operation which defines this address.
Definition Address.h:127
mlir::Value emitRawPointer() const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
Definition Address.h:98
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3328
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
U cast(CodeGen::Address addr)
Definition Address.h:327
#define false
Definition stdbool.h:26
static bool addressPointerAuthInfo()