clang 17.0.0git
Address.h
Go to the documentation of this file.
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 "clang/AST/CharUnits.h"
18#include "llvm/ADT/PointerIntPair.h"
19#include "llvm/IR/Constants.h"
20#include "llvm/Support/MathExtras.h"
21
22namespace clang {
23namespace CodeGen {
24
25// Indicates whether a pointer is known not to be null.
27
28/// An aligned address.
29class Address {
30 llvm::PointerIntPair<llvm::Value *, 1, bool> PointerAndKnownNonNull;
31 llvm::Type *ElementType;
32 CharUnits Alignment;
33
34protected:
35 Address(std::nullptr_t) : ElementType(nullptr) {}
36
37public:
38 Address(llvm::Value *Pointer, llvm::Type *ElementType, CharUnits Alignment,
39 KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
40 : PointerAndKnownNonNull(Pointer, IsKnownNonNull),
41 ElementType(ElementType), Alignment(Alignment) {
42 assert(Pointer != nullptr && "Pointer cannot be null");
43 assert(ElementType != nullptr && "Element type cannot be null");
44 assert(llvm::cast<llvm::PointerType>(Pointer->getType())
45 ->isOpaqueOrPointeeTypeMatches(ElementType) &&
46 "Incorrect pointer element type");
47 }
48
49 static Address invalid() { return Address(nullptr); }
50 bool isValid() const {
51 return PointerAndKnownNonNull.getPointer() != nullptr;
52 }
53
54 llvm::Value *getPointer() const {
55 assert(isValid());
56 return PointerAndKnownNonNull.getPointer();
57 }
58
59 /// Return the type of the pointer value.
60 llvm::PointerType *getType() const {
61 return llvm::cast<llvm::PointerType>(getPointer()->getType());
62 }
63
64 /// Return the type of the values stored in this address.
65 llvm::Type *getElementType() const {
66 assert(isValid());
67 return ElementType;
68 }
69
70 /// Return the address space that this address resides in.
71 unsigned getAddressSpace() const {
72 return getType()->getAddressSpace();
73 }
74
75 /// Return the IR name of the pointer value.
76 llvm::StringRef getName() const {
77 return getPointer()->getName();
78 }
79
80 /// Return the alignment of this pointer.
82 assert(isValid());
83 return Alignment;
84 }
85
86 /// Return address with different pointer, but same element type and
87 /// alignment.
88 Address withPointer(llvm::Value *NewPointer,
89 KnownNonNull_t IsKnownNonNull) const {
90 return Address(NewPointer, getElementType(), getAlignment(),
91 IsKnownNonNull);
92 }
93
94 /// Return address with different alignment, but same pointer and element
95 /// type.
96 Address withAlignment(CharUnits NewAlignment) const {
97 return Address(getPointer(), getElementType(), NewAlignment,
99 }
100
101 /// Whether the pointer is known not to be null.
103 assert(isValid());
104 return (KnownNonNull_t)PointerAndKnownNonNull.getInt();
105 }
106
107 /// Set the non-null bit.
109 assert(isValid());
110 PointerAndKnownNonNull.setInt(true);
111 return *this;
112 }
113};
114
115/// A specialization of Address that requires the address to be an
116/// LLVM Constant.
117class ConstantAddress : public Address {
118 ConstantAddress(std::nullptr_t) : Address(nullptr) {}
119
120public:
121 ConstantAddress(llvm::Constant *pointer, llvm::Type *elementType,
122 CharUnits alignment)
123 : Address(pointer, elementType, alignment) {}
124
126 return ConstantAddress(nullptr);
127 }
128
129 llvm::Constant *getPointer() const {
130 return llvm::cast<llvm::Constant>(Address::getPointer());
131 }
132
133 ConstantAddress getElementBitCast(llvm::Type *ElemTy) const {
134 llvm::Constant *BitCast = llvm::ConstantExpr::getBitCast(
135 getPointer(), ElemTy->getPointerTo(getAddressSpace()));
136 return ConstantAddress(BitCast, ElemTy, getAlignment());
137 }
138
139 static bool isaImpl(Address addr) {
140 return llvm::isa<llvm::Constant>(addr.getPointer());
141 }
143 return ConstantAddress(llvm::cast<llvm::Constant>(addr.getPointer()),
144 addr.getElementType(), addr.getAlignment());
145 }
146};
147
148}
149
150// Present a minimal LLVM-like casting interface.
151template <class U> inline U cast(CodeGen::Address addr) {
152 return U::castImpl(addr);
153}
154template <class U> inline bool isa(CodeGen::Address addr) {
155 return U::isaImpl(addr);
156}
157
158}
159
160#endif
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
An aligned address.
Definition: Address.h:29
Address(std::nullptr_t)
Definition: Address.h:35
static Address invalid()
Definition: Address.h:49
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:81
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:65
Address withPointer(llvm::Value *NewPointer, KnownNonNull_t IsKnownNonNull) const
Return address with different pointer, but same element type and alignment.
Definition: Address.h:88
unsigned getAddressSpace() const
Return the address space that this address resides in.
Definition: Address.h:71
KnownNonNull_t isKnownNonNull() const
Whether the pointer is known not to be null.
Definition: Address.h:102
Address setKnownNonNull()
Set the non-null bit.
Definition: Address.h:108
Address withAlignment(CharUnits NewAlignment) const
Return address with different alignment, but same pointer and element type.
Definition: Address.h:96
Address(llvm::Value *Pointer, llvm::Type *ElementType, CharUnits Alignment, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
Definition: Address.h:38
llvm::Value * getPointer() const
Definition: Address.h:54
llvm::StringRef getName() const
Return the IR name of the pointer value.
Definition: Address.h:76
bool isValid() const
Definition: Address.h:50
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:60
A specialization of Address that requires the address to be an LLVM Constant.
Definition: Address.h:117
ConstantAddress getElementBitCast(llvm::Type *ElemTy) const
Definition: Address.h:133
static bool isaImpl(Address addr)
Definition: Address.h:139
static ConstantAddress castImpl(Address addr)
Definition: Address.h:142
static ConstantAddress invalid()
Definition: Address.h:125
ConstantAddress(llvm::Constant *pointer, llvm::Type *elementType, CharUnits alignment)
Definition: Address.h:121
llvm::Constant * getPointer() const
Definition: Address.h:129
@ NotKnownNonNull
Definition: Address.h:26
bool isa(CodeGen::Address addr)
Definition: Address.h:154
U cast(CodeGen::Address addr)
Definition: Address.h:151