[llvm-commits] [llvm] r79208 - in /llvm/trunk: include/llvm/Constants.h lib/VMCore/Constants.cpp
Dan Gohman
gohman at apple.com
Sun Aug 16 14:26:11 PDT 2009
Author: djg
Date: Sun Aug 16 16:26:11 2009
New Revision: 79208
URL: http://llvm.org/viewvc/llvm-project?rev=79208&view=rev
Log:
Add a getOffsetOf, for building a target-independent expression for
offsetof, similar to getSizeOf for sizeof.
Modified:
llvm/trunk/include/llvm/Constants.h
llvm/trunk/lib/VMCore/Constants.cpp
Modified: llvm/trunk/include/llvm/Constants.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Constants.h?rev=79208&r1=79207&r2=79208&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Constants.h (original)
+++ llvm/trunk/include/llvm/Constants.h Sun Aug 16 16:26:11 2009
@@ -602,6 +602,11 @@
/// 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);
Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=79208&r1=79207&r2=79208&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Sun Aug 16 16:26:11 2009
@@ -1391,6 +1391,18 @@
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) {
More information about the llvm-commits
mailing list