summaryrefslogtreecommitdiffstats
path: root/llvm/lib/VMCore/Constants.cpp
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
commitff3af725eaca0cb1d27d5c0b1d2d3383ecb76ca9 (patch)
treebd02e75f231641c25c419f28315c60811e988d82 /llvm/lib/VMCore/Constants.cpp
parentf0b984021c6d95eb323eabe6cb536afe3ef68f1a (diff)
downloadbcm5719-llvm-ff3af725eaca0cb1d27d5c0b1d2d3383ecb76ca9.tar.gz
bcm5719-llvm-ff3af725eaca0cb1d27d5c0b1d2d3383ecb76ca9.zip
Add a getOffsetOf, for building a target-independent expression for
offsetof, similar to getSizeOf for sizeof. llvm-svn: 79208
Diffstat (limited to 'llvm/lib/VMCore/Constants.cpp')
-rw-r--r--llvm/lib/VMCore/Constants.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp
index 647bc1225a2..f820033932d 100644
--- a/llvm/lib/VMCore/Constants.cpp
+++ b/llvm/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) {
OpenPOWER on IntegriCloud