1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 03:33:20 +01:00

Add a getOffsetOf, for building a target-independent expression for

offsetof, similar to getSizeOf for sizeof.

llvm-svn: 79208
This commit is contained in:
Dan Gohman 2009-08-16 21:26:11 +00:00
parent 8a92a2475c
commit a7f21f1767
2 changed files with 17 additions and 0 deletions

View File

@ -602,6 +602,11 @@ public:
/// independent way (Note: the return type is an i64).
///
static Constant* getSizeOf(const Type* Ty);
/// getOffsetOf constant expr - computes the offset of a field in a target
/// independent way (Note: the return type is an i64).
///
static Constant* getOffsetOf(const StructType* Ty, unsigned FieldNo);
static Constant* getNeg(Constant* C);
static Constant* getFNeg(Constant* C);

View File

@ -1391,6 +1391,18 @@ Constant* ConstantExpr::getAlignOf(const Type* Ty) {
Type::getInt32Ty(Ty->getContext()));
}
Constant* ConstantExpr::getOffsetOf(const StructType* STy, unsigned FieldNo) {
// offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
// Note that a non-inbounds gep is used, as null isn't within any object.
Constant *GEPIdx[] = {
ConstantInt::get(Type::getInt64Ty(STy->getContext()), 0),
ConstantInt::get(Type::getInt32Ty(STy->getContext()), FieldNo)
};
Constant *GEP = getGetElementPtr(
Constant::getNullValue(PointerType::getUnqual(STy)), GEPIdx, 2);
return getCast(Instruction::PtrToInt, GEP,
Type::getInt64Ty(STy->getContext()));
}
Constant *ConstantExpr::getCompare(unsigned short pred,
Constant *C1, Constant *C2) {