@@ -185,6 +185,81 @@ class Address {
185185 }
186186};
187187
188+ // / An abstract representation of an aligned address. This is designed to be an
189+ // / IR-level abstraction, carrying just the information necessary to perform IR
190+ // / operations on an address like loads and stores. In particular, it doesn't
191+ // / carry C type information or allow the representation of things like
192+ // / bit-fields; clients working at that level should generally be using
193+ // / `LValue`.
194+ // / The pointer contained in this class is known to be unsigned.
195+ class RawAddress {
196+ llvm::PointerIntPair<mlir::Value *, 1 , bool > PointerAndKnownNonNull;
197+ mlir::Type *ElementType;
198+ CharUnits Alignment;
199+
200+ protected:
201+ RawAddress (std::nullptr_t ) : ElementType(nullptr ) {}
202+
203+ public:
204+ RawAddress (mlir::Value *Pointer, mlir::Type *ElementType, CharUnits Alignment,
205+ KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
206+ : PointerAndKnownNonNull(Pointer, IsKnownNonNull),
207+ ElementType (ElementType), Alignment(Alignment) {
208+ assert (Pointer != nullptr && " Pointer cannot be null" );
209+ assert (ElementType != nullptr && " Element type cannot be null" );
210+ }
211+
212+ inline RawAddress (Address Addr);
213+
214+ static RawAddress invalid () { return RawAddress (nullptr ); }
215+ bool isValid () const {
216+ return PointerAndKnownNonNull.getPointer () != nullptr ;
217+ }
218+
219+ mlir::Value *getPointer () const {
220+ assert (isValid ());
221+ return PointerAndKnownNonNull.getPointer ();
222+ }
223+
224+ // / Return the type of the pointer value.
225+ llvm::PointerType *getType () const {
226+ return llvm::cast<llvm::PointerType>(getPointer ()->getType ());
227+ }
228+
229+ // / Return the type of the values stored in this address.
230+ mlir::Type *getElementType () const {
231+ assert (isValid ());
232+ return ElementType;
233+ }
234+
235+ // / Return the address space that this address resides in.
236+ unsigned getAddressSpace () const {
237+ return getType ()->getAddressSpace ();
238+ }
239+
240+ // / Return the IR name of the pointer value.
241+ llvm::StringRef getName () const {
242+ return getPointer ()->getName ();
243+ }
244+
245+ // / Return the alignment of this pointer.
246+ CharUnits getAlignment () const {
247+ assert (isValid ());
248+ return Alignment;
249+ }
250+
251+ // / Return address with different element type, but same pointer and
252+ // / alignment.
253+ RawAddress withElementType (mlir::Type *ElemTy) const {
254+ return RawAddress (getPointer (), ElemTy, getAlignment (), isKnownNonNull ());
255+ }
256+
257+ KnownNonNull_t isKnownNonNull () const {
258+ assert (isValid ());
259+ return (KnownNonNull_t)PointerAndKnownNonNull.getInt ();
260+ }
261+ };
262+
188263} // namespace clang::CIRGen
189264
190265#endif // LLVM_CLANG_LIB_CIR_ADDRESS_H
0 commit comments