15
15
#define LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
16
16
17
17
#include " clang/AST/CharUnits.h"
18
- #include " clang/AST/Type.h"
19
18
#include " llvm/ADT/PointerIntPair.h"
20
19
#include " llvm/IR/Constants.h"
21
20
#include " llvm/Support/MathExtras.h"
22
21
23
22
namespace clang {
24
23
namespace CodeGen {
25
24
26
- class Address ;
27
- class CGBuilderTy ;
28
- class CodeGenFunction ;
29
- class CodeGenModule ;
30
-
31
25
// Indicates whether a pointer is known not to be null.
32
26
enum KnownNonNull_t { NotKnownNonNull, KnownNonNull };
33
27
34
- // / An abstract representation of an aligned address. This is designed to be an
35
- // / IR-level abstraction, carrying just the information necessary to perform IR
36
- // / operations on an address like loads and stores. In particular, it doesn't
37
- // / carry C type information or allow the representation of things like
38
- // / bit-fields; clients working at that level should generally be using
39
- // / `LValue`.
40
- // / The pointer contained in this class is known to be unsigned.
41
- class RawAddress {
28
+ // / An aligned address.
29
+ class Address {
42
30
llvm::PointerIntPair<llvm::Value *, 1 , bool > PointerAndKnownNonNull;
43
31
llvm::Type *ElementType;
44
32
CharUnits Alignment;
45
33
46
34
protected:
47
- RawAddress (std::nullptr_t ) : ElementType(nullptr ) {}
35
+ Address (std::nullptr_t ) : ElementType(nullptr ) {}
48
36
49
37
public:
50
- RawAddress (llvm::Value *Pointer, llvm::Type *ElementType, CharUnits Alignment,
51
- KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
38
+ Address (llvm::Value *Pointer, llvm::Type *ElementType, CharUnits Alignment,
39
+ KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
52
40
: PointerAndKnownNonNull(Pointer, IsKnownNonNull),
53
41
ElementType (ElementType), Alignment(Alignment) {
54
42
assert (Pointer != nullptr && " Pointer cannot be null" );
55
43
assert (ElementType != nullptr && " Element type cannot be null" );
56
44
}
57
45
58
- inline RawAddress (Address Addr);
59
-
60
- static RawAddress invalid () { return RawAddress (nullptr ); }
46
+ static Address invalid () { return Address (nullptr ); }
61
47
bool isValid () const {
62
48
return PointerAndKnownNonNull.getPointer () != nullptr ;
63
49
}
@@ -94,133 +80,6 @@ class RawAddress {
94
80
return Alignment;
95
81
}
96
82
97
- // / Return address with different element type, but same pointer and
98
- // / alignment.
99
- RawAddress withElementType (llvm::Type *ElemTy) const {
100
- return RawAddress (getPointer (), ElemTy, getAlignment (), isKnownNonNull ());
101
- }
102
-
103
- KnownNonNull_t isKnownNonNull () const {
104
- assert (isValid ());
105
- return (KnownNonNull_t)PointerAndKnownNonNull.getInt ();
106
- }
107
- };
108
-
109
- // / Like RawAddress, an abstract representation of an aligned address, but the
110
- // / pointer contained in this class is possibly signed.
111
- class Address {
112
- friend class CGBuilderTy ;
113
-
114
- // The boolean flag indicates whether the pointer is known to be non-null.
115
- llvm::PointerIntPair<llvm::Value *, 1 , bool > Pointer;
116
-
117
- // / The expected IR type of the pointer. Carrying accurate element type
118
- // / information in Address makes it more convenient to work with Address
119
- // / values and allows frontend assertions to catch simple mistakes.
120
- llvm::Type *ElementType = nullptr ;
121
-
122
- CharUnits Alignment;
123
-
124
- // / Offset from the base pointer.
125
- llvm::Value *Offset = nullptr ;
126
-
127
- llvm::Value *emitRawPointerSlow (CodeGenFunction &CGF) const ;
128
-
129
- protected:
130
- Address (std::nullptr_t ) : ElementType(nullptr ) {}
131
-
132
- public:
133
- Address (llvm::Value *pointer, llvm::Type *elementType, CharUnits alignment,
134
- KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
135
- : Pointer(pointer, IsKnownNonNull), ElementType(elementType),
136
- Alignment (alignment) {
137
- assert (pointer != nullptr && " Pointer cannot be null" );
138
- assert (elementType != nullptr && " Element type cannot be null" );
139
- assert (!alignment.isZero () && " Alignment cannot be zero" );
140
- }
141
-
142
- Address (llvm::Value *BasePtr, llvm::Type *ElementType, CharUnits Alignment,
143
- llvm::Value *Offset, KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
144
- : Pointer(BasePtr, IsKnownNonNull), ElementType(ElementType),
145
- Alignment(Alignment), Offset(Offset) {}
146
-
147
- Address (RawAddress RawAddr)
148
- : Pointer(RawAddr.isValid() ? RawAddr.getPointer() : nullptr),
149
- ElementType(RawAddr.isValid() ? RawAddr.getElementType() : nullptr),
150
- Alignment(RawAddr.isValid() ? RawAddr.getAlignment()
151
- : CharUnits::Zero()) {}
152
-
153
- static Address invalid () { return Address (nullptr ); }
154
- bool isValid () const { return Pointer.getPointer () != nullptr ; }
155
-
156
- // / This function is used in situations where the caller is doing some sort of
157
- // / opaque "laundering" of the pointer.
158
- void replaceBasePointer (llvm::Value *P) {
159
- assert (isValid () && " pointer isn't valid" );
160
- assert (P->getType () == Pointer.getPointer ()->getType () &&
161
- " Pointer's type changed" );
162
- Pointer.setPointer (P);
163
- assert (isValid () && " pointer is invalid after replacement" );
164
- }
165
-
166
- CharUnits getAlignment () const { return Alignment; }
167
-
168
- void setAlignment (CharUnits Value) { Alignment = Value; }
169
-
170
- llvm::Value *getBasePointer () const {
171
- assert (isValid () && " pointer isn't valid" );
172
- return Pointer.getPointer ();
173
- }
174
-
175
- // / Return the type of the pointer value.
176
- llvm::PointerType *getType () const {
177
- return llvm::PointerType::get (
178
- ElementType,
179
- llvm::cast<llvm::PointerType>(Pointer.getPointer ()->getType ())
180
- ->getAddressSpace ());
181
- }
182
-
183
- // / Return the type of the values stored in this address.
184
- llvm::Type *getElementType () const {
185
- assert (isValid ());
186
- return ElementType;
187
- }
188
-
189
- // / Return the address space that this address resides in.
190
- unsigned getAddressSpace () const { return getType ()->getAddressSpace (); }
191
-
192
- // / Return the IR name of the pointer value.
193
- llvm::StringRef getName () const { return Pointer.getPointer ()->getName (); }
194
-
195
- // This function is called only in CGBuilderBaseTy::CreateElementBitCast.
196
- void setElementType (llvm::Type *Ty) {
197
- assert (hasOffset () &&
198
- " this funcion shouldn't be called when there is no offset" );
199
- ElementType = Ty;
200
- }
201
-
202
- // / Whether the pointer is known not to be null.
203
- KnownNonNull_t isKnownNonNull () const {
204
- assert (isValid ());
205
- return (KnownNonNull_t)Pointer.getInt ();
206
- }
207
-
208
- Address setKnownNonNull () {
209
- assert (isValid ());
210
- Pointer.setInt (KnownNonNull);
211
- return *this ;
212
- }
213
-
214
- bool hasOffset () const { return Offset; }
215
-
216
- llvm::Value *getOffset () const { return Offset; }
217
-
218
- // / Return the pointer contained in this class after authenticating it and
219
- // / adding offset to it if necessary.
220
- llvm::Value *emitRawPointer (CodeGenFunction &CGF) const {
221
- return getBasePointer ();
222
- }
223
-
224
83
// / Return address with different pointer, but same element type and
225
84
// / alignment.
226
85
Address withPointer (llvm::Value *NewPointer,
@@ -232,59 +91,61 @@ class Address {
232
91
// / Return address with different alignment, but same pointer and element
233
92
// / type.
234
93
Address withAlignment (CharUnits NewAlignment) const {
235
- return Address (Pointer. getPointer (), getElementType (), NewAlignment,
94
+ return Address (getPointer (), getElementType (), NewAlignment,
236
95
isKnownNonNull ());
237
96
}
238
97
239
98
// / Return address with different element type, but same pointer and
240
99
// / alignment.
241
100
Address withElementType (llvm::Type *ElemTy) const {
242
- if (!hasOffset ())
243
- return Address (getBasePointer (), ElemTy, getAlignment (), nullptr ,
244
- isKnownNonNull ());
245
- Address A (*this );
246
- A.ElementType = ElemTy;
247
- return A;
101
+ return Address (getPointer (), ElemTy, getAlignment (), isKnownNonNull ());
248
102
}
249
- };
250
103
251
- inline RawAddress::RawAddress (Address Addr)
252
- : PointerAndKnownNonNull(Addr.isValid() ? Addr.getBasePointer() : nullptr,
253
- Addr.isValid() ? Addr.isKnownNonNull()
254
- : NotKnownNonNull),
255
- ElementType(Addr.isValid() ? Addr.getElementType() : nullptr),
256
- Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
104
+ // / Whether the pointer is known not to be null.
105
+ KnownNonNull_t isKnownNonNull () const {
106
+ assert (isValid ());
107
+ return (KnownNonNull_t)PointerAndKnownNonNull.getInt ();
108
+ }
109
+
110
+ // / Set the non-null bit.
111
+ Address setKnownNonNull () {
112
+ assert (isValid ());
113
+ PointerAndKnownNonNull.setInt (true );
114
+ return *this ;
115
+ }
116
+ };
257
117
258
118
// / A specialization of Address that requires the address to be an
259
119
// / LLVM Constant.
260
- class ConstantAddress : public RawAddress {
261
- ConstantAddress (std::nullptr_t ) : RawAddress (nullptr ) {}
120
+ class ConstantAddress : public Address {
121
+ ConstantAddress (std::nullptr_t ) : Address (nullptr ) {}
262
122
263
123
public:
264
124
ConstantAddress (llvm::Constant *pointer, llvm::Type *elementType,
265
125
CharUnits alignment)
266
- : RawAddress (pointer, elementType, alignment) {}
126
+ : Address (pointer, elementType, alignment) {}
267
127
268
128
static ConstantAddress invalid () {
269
129
return ConstantAddress (nullptr );
270
130
}
271
131
272
132
llvm::Constant *getPointer () const {
273
- return llvm::cast<llvm::Constant>(RawAddress ::getPointer ());
133
+ return llvm::cast<llvm::Constant>(Address ::getPointer ());
274
134
}
275
135
276
136
ConstantAddress withElementType (llvm::Type *ElemTy) const {
277
137
return ConstantAddress (getPointer (), ElemTy, getAlignment ());
278
138
}
279
139
280
- static bool isaImpl (RawAddress addr) {
140
+ static bool isaImpl (Address addr) {
281
141
return llvm::isa<llvm::Constant>(addr.getPointer ());
282
142
}
283
- static ConstantAddress castImpl (RawAddress addr) {
143
+ static ConstantAddress castImpl (Address addr) {
284
144
return ConstantAddress (llvm::cast<llvm::Constant>(addr.getPointer ()),
285
145
addr.getElementType (), addr.getAlignment ());
286
146
}
287
147
};
148
+
288
149
}
289
150
290
151
// Present a minimal LLVM-like casting interface.
0 commit comments