summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorDuncan Sands <baldrick@free.fr>2009-05-21 15:52:21 +0000
committerDuncan Sands <baldrick@free.fr>2009-05-21 15:52:21 +0000
commitd334aca93f538d1a59ae3296fd7bddf5766de2a7 (patch)
treeb61ee358ec5217c30a2454ec8e59aa275602076f /llvm/lib
parent027de2adcd43f01221db121ada17f11e0bc8a543 (diff)
downloadbcm5719-llvm-d334aca93f538d1a59ae3296fd7bddf5766de2a7.tar.gz
bcm5719-llvm-d334aca93f538d1a59ae3296fd7bddf5766de2a7.zip
Add a getAlignOf helper for getting the ABI alignment of a
type as a target independent constant expression. I confess that I didn't check that this method works as intended (though I did test the equivalent hand-written IR a little). But what could possibly go wrong! llvm-svn: 72213
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/VMCore/Constants.cpp11
-rw-r--r--llvm/lib/VMCore/Core.cpp4
2 files changed, 15 insertions, 0 deletions
diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp
index b38474d28a4..d3f9b7f45b7 100644
--- a/llvm/lib/VMCore/Constants.cpp
+++ b/llvm/lib/VMCore/Constants.cpp
@@ -2082,6 +2082,17 @@ Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
return getFoldedCast(Instruction::BitCast, C, DstTy);
}
+Constant *ConstantExpr::getAlignOf(const Type *Ty) {
+ // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
+ const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
+ Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
+ Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
+ Constant *One = ConstantInt::get(Type::Int32Ty, 1);
+ Constant *Indices[2] = { Zero, One };
+ Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
+ return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
+}
+
Constant *ConstantExpr::getSizeOf(const Type *Ty) {
// sizeof is implemented as: (i64) gep (Ty*)null, 1
Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
diff --git a/llvm/lib/VMCore/Core.cpp b/llvm/lib/VMCore/Core.cpp
index 1fa83ebd1c0..f85dbe76119 100644
--- a/llvm/lib/VMCore/Core.cpp
+++ b/llvm/lib/VMCore/Core.cpp
@@ -356,6 +356,10 @@ LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
/*--.. Constant expressions ................................................--*/
+LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
+ return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
+}
+
LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
}
OpenPOWER on IntegriCloud