Skip to content

Commit 3aed71e

Browse files
committed
Push progress
1 parent bffbae5 commit 3aed71e

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

clang/lib/CIR/CodeGen/Address.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,19 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
729729
return Address(derivedAddr, destType, addr.getAlignment());
730730
}
731731

732+
Address CreateInBoundsGEP(Address Addr, ArrayRef<llvm::Value *> IdxList,
733+
llvm::Type *ElementType, CharUnits Align,
734+
const Twine &Name = "") {
735+
return RawAddress(CreateInBoundsGEP(Addr.getElementType(),
736+
emitRawPointerFromAddress(Addr),
737+
IdxList, Name),
738+
ElementType, Align, Addr.isKnownNonNull());
739+
}
740+
741+
mlir::Value emitRawPointerFromAddress(Address Addr) const {
742+
return Addr.getBasePointer();
743+
}
744+
732745
Address createInBoundsGEP(Address Addr, ArrayRef<llvm::Value *> IdxList,
733746
llvm::Type *ElementType, CharUnits Align,
734747
const Twine &Name = "") {

0 commit comments

Comments
 (0)