1//===-- Address.h - An aligned address -------------------------*- C++ -*-===//
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 LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
15#define LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
16
17#include "CGPointerAuthInfo.h"
18#include "clang/AST/CharUnits.h"
19#include "clang/AST/Type.h"
20#include "llvm/ADT/PointerIntPair.h"
21#include "llvm/IR/Constants.h"
22#include "llvm/Support/MathExtras.h"
23
24namespace clang {
25namespace CodeGen {
26
27class Address;
28class CGBuilderTy;
29class CodeGenFunction;
30class CodeGenModule;
31
32// Indicates whether a pointer is known not to be null.
33enum KnownNonNull_t { NotKnownNonNull, KnownNonNull };
34
35/// An abstract representation of an aligned address. This is designed to be an
36/// IR-level abstraction, carrying just the information necessary to perform IR
37/// operations on an address like loads and stores. In particular, it doesn't
38/// carry C type information or allow the representation of things like
39/// bit-fields; clients working at that level should generally be using
40/// `LValue`.
41/// The pointer contained in this class is known to be unsigned.
42class RawAddress {
43 llvm::PointerIntPair<llvm::Value *, 1, bool> PointerAndKnownNonNull;
44 llvm::Type *ElementType;
45 CharUnits Alignment;
46
47protected:
48 RawAddress(std::nullptr_t) : ElementType(nullptr) {}
49
50public:
51 RawAddress(llvm::Value *Pointer, llvm::Type *ElementType, CharUnits Alignment,
52 KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
53 : PointerAndKnownNonNull(Pointer, IsKnownNonNull),
54 ElementType(ElementType), Alignment(Alignment) {
55 assert(Pointer != nullptr && "Pointer cannot be null");
56 assert(ElementType != nullptr && "Element type cannot be null");
57 }
58
59 inline RawAddress(Address Addr);
60
61 static RawAddress invalid() { return RawAddress(nullptr); }
62 bool isValid() const {
63 return PointerAndKnownNonNull.getPointer() != nullptr;
64 }
65
66 llvm::Value *getPointer() const {
67 assert(isValid());
68 return PointerAndKnownNonNull.getPointer();
69 }
70
71 /// Return the type of the pointer value.
72 llvm::PointerType *getType() const {
73 return llvm::cast<llvm::PointerType>(Val: getPointer()->getType());
74 }
75
76 /// Return the type of the values stored in this address.
77 llvm::Type *getElementType() const {
78 assert(isValid());
79 return ElementType;
80 }
81
82 /// Return the address space that this address resides in.
83 unsigned getAddressSpace() const {
84 return getType()->getAddressSpace();
85 }
86
87 /// Return the IR name of the pointer value.
88 llvm::StringRef getName() const {
89 return getPointer()->getName();
90 }
91
92 /// Return the alignment of this pointer.
93 CharUnits getAlignment() const {
94 assert(isValid());
95 return Alignment;
96 }
97
98 /// Return address with different element type, but same pointer and
99 /// alignment.
100 RawAddress withElementType(llvm::Type *ElemTy) const {
101 return RawAddress(getPointer(), ElemTy, getAlignment(), isKnownNonNull());
102 }
103
104 KnownNonNull_t isKnownNonNull() const {
105 assert(isValid());
106 return (KnownNonNull_t)PointerAndKnownNonNull.getInt();
107 }
108};
109
110/// Like RawAddress, an abstract representation of an aligned address, but the
111/// pointer contained in this class is possibly signed.
112///
113/// This is designed to be an IR-level abstraction, carrying just the
114/// information necessary to perform IR operations on an address like loads and
115/// stores. In particular, it doesn't carry C type information or allow the
116/// representation of things like bit-fields; clients working at that level
117/// should generally be using `LValue`.
118///
119/// An address may be either *raw*, meaning that it's an ordinary machine
120/// pointer, or *signed*, meaning that the pointer carries an embedded
121/// pointer-authentication signature. Representing signed pointers directly in
122/// this abstraction allows the authentication to be delayed as long as possible
123/// without forcing IRGen to use totally different code paths for signed and
124/// unsigned values or to separately propagate signature information through
125/// every API that manipulates addresses. Pointer arithmetic on signed addresses
126/// (e.g. drilling down to a struct field) is accumulated into a separate offset
127/// which is applied when the address is finally accessed.
128class Address {
129 friend class CGBuilderTy;
130
131 // The boolean flag indicates whether the pointer is known to be non-null.
132 llvm::PointerIntPair<llvm::Value *, 1, bool> Pointer;
133
134 /// The expected IR type of the pointer. Carrying accurate element type
135 /// information in Address makes it more convenient to work with Address
136 /// values and allows frontend assertions to catch simple mistakes.
137 llvm::Type *ElementType = nullptr;
138
139 CharUnits Alignment;
140
141 /// The ptrauth information needed to authenticate the base pointer.
142 CGPointerAuthInfo PtrAuthInfo;
143
144 /// Offset from the base pointer. This is non-null only when the base
145 /// pointer is signed.
146 llvm::Value *Offset = nullptr;
147
148 llvm::Value *emitRawPointerSlow(CodeGenFunction &CGF) const;
149
150protected:
151 Address(std::nullptr_t) : ElementType(nullptr) {}
152
153public:
154 Address(llvm::Value *pointer, llvm::Type *elementType, CharUnits alignment,
155 KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
156 : Pointer(pointer, IsKnownNonNull), ElementType(elementType),
157 Alignment(alignment) {
158 assert(pointer != nullptr && "Pointer cannot be null");
159 assert(elementType != nullptr && "Element type cannot be null");
160 assert(!alignment.isZero() && "Alignment cannot be zero");
161 }
162
163 Address(llvm::Value *BasePtr, llvm::Type *ElementType, CharUnits Alignment,
164 CGPointerAuthInfo PtrAuthInfo, llvm::Value *Offset,
165 KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
166 : Pointer(BasePtr, IsKnownNonNull), ElementType(ElementType),
167 Alignment(Alignment), PtrAuthInfo(PtrAuthInfo), Offset(Offset) {}
168
169 Address(RawAddress RawAddr)
170 : Pointer(RawAddr.isValid() ? RawAddr.getPointer() : nullptr,
171 RawAddr.isValid() ? RawAddr.isKnownNonNull() : NotKnownNonNull),
172 ElementType(RawAddr.isValid() ? RawAddr.getElementType() : nullptr),
173 Alignment(RawAddr.isValid() ? RawAddr.getAlignment()
174 : CharUnits::Zero()) {}
175
176 static Address invalid() { return Address(nullptr); }
177 bool isValid() const { return Pointer.getPointer() != nullptr; }
178
179 /// This function is used in situations where the caller is doing some sort of
180 /// opaque "laundering" of the pointer.
181 void replaceBasePointer(llvm::Value *P) {
182 assert(isValid() && "pointer isn't valid");
183 assert(P->getType() == Pointer.getPointer()->getType() &&
184 "Pointer's type changed");
185 Pointer.setPointer(P);
186 assert(isValid() && "pointer is invalid after replacement");
187 }
188
189 CharUnits getAlignment() const { return Alignment; }
190
191 void setAlignment(CharUnits Value) { Alignment = Value; }
192
193 llvm::Value *getBasePointer() const {
194 assert(isValid() && "pointer isn't valid");
195 return Pointer.getPointer();
196 }
197
198 /// Return the type of the pointer value.
199 llvm::PointerType *getType() const {
200 return llvm::cast<llvm::PointerType>(Val: Pointer.getPointer()->getType());
201 }
202
203 /// Return the type of the values stored in this address.
204 llvm::Type *getElementType() const {
205 assert(isValid());
206 return ElementType;
207 }
208
209 /// Return the address space that this address resides in.
210 unsigned getAddressSpace() const { return getType()->getAddressSpace(); }
211
212 /// Return the IR name of the pointer value.
213 llvm::StringRef getName() const { return Pointer.getPointer()->getName(); }
214
215 const CGPointerAuthInfo &getPointerAuthInfo() const { return PtrAuthInfo; }
216 void setPointerAuthInfo(const CGPointerAuthInfo &Info) { PtrAuthInfo = Info; }
217
218 // This function is called only in CGBuilderBaseTy::CreateElementBitCast.
219 void setElementType(llvm::Type *Ty) {
220 assert(hasOffset() &&
221 "this funcion shouldn't be called when there is no offset");
222 ElementType = Ty;
223 }
224
225 bool isSigned() const { return PtrAuthInfo.isSigned(); }
226
227 /// Whether the pointer is known not to be null.
228 KnownNonNull_t isKnownNonNull() const {
229 assert(isValid());
230 return (KnownNonNull_t)Pointer.getInt();
231 }
232
233 Address setKnownNonNull() {
234 assert(isValid());
235 Pointer.setInt(KnownNonNull);
236 return *this;
237 }
238
239 bool hasOffset() const { return Offset; }
240
241 llvm::Value *getOffset() const { return Offset; }
242
243 Address getResignedAddress(const CGPointerAuthInfo &NewInfo,
244 CodeGenFunction &CGF) const;
245
246 /// Return the pointer contained in this class after authenticating it and
247 /// adding offset to it if necessary.
248 llvm::Value *emitRawPointer(CodeGenFunction &CGF) const {
249 if (!isSigned())
250 return getBasePointer();
251 return emitRawPointerSlow(CGF);
252 }
253
254 /// Return address with different pointer, but same element type and
255 /// alignment.
256 Address withPointer(llvm::Value *NewPointer,
257 KnownNonNull_t IsKnownNonNull) const {
258 return Address(NewPointer, getElementType(), getAlignment(),
259 IsKnownNonNull);
260 }
261
262 /// Return address with different alignment, but same pointer and element
263 /// type.
264 Address withAlignment(CharUnits NewAlignment) const {
265 return Address(Pointer.getPointer(), getElementType(), NewAlignment,
266 isKnownNonNull());
267 }
268
269 /// Return address with different element type, but same pointer and
270 /// alignment.
271 Address withElementType(llvm::Type *ElemTy) const {
272 if (!hasOffset())
273 return Address(getBasePointer(), ElemTy, getAlignment(),
274 getPointerAuthInfo(), /*Offset=*/nullptr,
275 isKnownNonNull());
276 Address A(*this);
277 A.ElementType = ElemTy;
278 return A;
279 }
280};
281
282inline RawAddress::RawAddress(Address Addr)
283 : PointerAndKnownNonNull(Addr.isValid() ? Addr.getBasePointer() : nullptr,
284 Addr.isValid() ? Addr.isKnownNonNull()
285 : NotKnownNonNull),
286 ElementType(Addr.isValid() ? Addr.getElementType() : nullptr),
287 Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
288
289/// A specialization of Address that requires the address to be an
290/// LLVM Constant.
291class ConstantAddress : public RawAddress {
292 ConstantAddress(std::nullptr_t) : RawAddress(nullptr) {}
293
294public:
295 ConstantAddress(llvm::Constant *pointer, llvm::Type *elementType,
296 CharUnits alignment)
297 : RawAddress(pointer, elementType, alignment) {}
298
299 static ConstantAddress invalid() {
300 return ConstantAddress(nullptr);
301 }
302
303 llvm::Constant *getPointer() const {
304 return llvm::cast<llvm::Constant>(Val: RawAddress::getPointer());
305 }
306
307 ConstantAddress withElementType(llvm::Type *ElemTy) const {
308 return ConstantAddress(getPointer(), ElemTy, getAlignment());
309 }
310
311 static bool isaImpl(RawAddress addr) {
312 return llvm::isa<llvm::Constant>(Val: addr.getPointer());
313 }
314 static ConstantAddress castImpl(RawAddress addr) {
315 return ConstantAddress(llvm::cast<llvm::Constant>(Val: addr.getPointer()),
316 addr.getElementType(), addr.getAlignment());
317 }
318};
319}
320
321// Present a minimal LLVM-like casting interface.
322template <class U> inline U cast(CodeGen::Address addr) {
323 return U::castImpl(addr);
324}
325template <class U> inline bool isa(CodeGen::Address addr) {
326 return U::isaImpl(addr);
327}
328
329}
330
331#endif
332