aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/llvm/Constants.h5
-rw-r--r--lib/VMCore/Constants.cpp12
2 files changed, 17 insertions, 0 deletions
diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h
index e26bb62c35..a66773c0b5 100644
--- a/include/llvm/Constants.h
+++ b/include/llvm/Constants.h
@@ -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);
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp
index 647bc1225a..f820033932 100644
--- a/lib/VMCore/Constants.cpp
+++ b/lib/VMCore/Constants.cpp
@@ -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) {