aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDan Gohman <gohman@apple.com>2009-08-16 21:26:11 +0000
committerDan Gohman <gohman@apple.com>2009-08-16 21:26:11 +0000
commit3778f21b17e675c2fd1600901361fa4a73888eba (patch)
treee975284da0629a07af5221e21342860c2678c4c4 /lib
parentface41a4de759e881bea1769d86c4cfda2cd1545 (diff)
downloadexternal_llvm-3778f21b17e675c2fd1600901361fa4a73888eba.zip
external_llvm-3778f21b17e675c2fd1600901361fa4a73888eba.tar.gz
external_llvm-3778f21b17e675c2fd1600901361fa4a73888eba.tar.bz2
Add a getOffsetOf, for building a target-independent expression for
offsetof, similar to getSizeOf for sizeof. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79208 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/VMCore/Constants.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp
index 647bc12..f820033 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) {