aboutsummaryrefslogtreecommitdiffstats
path: root/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
commit788f65ea894f8e607fdf41f8c360bc9d1d987153 (patch)
tree1ffe037dbd917d3dc1434c1947079f899fcf51d8 /lib
parent2d9b3cbe342bca2e2ec06f74e90a949c43b89fce (diff)
downloadexternal_llvm-788f65ea894f8e607fdf41f8c360bc9d1d987153.zip
external_llvm-788f65ea894f8e607fdf41f8c360bc9d1d987153.tar.gz
external_llvm-788f65ea894f8e607fdf41f8c360bc9d1d987153.tar.bz2
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! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@72213 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/VMCore/Constants.cpp11
-rw-r--r--lib/VMCore/Core.cpp4
2 files changed, 15 insertions, 0 deletions
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp
index b38474d..d3f9b7f 100644
--- a/lib/VMCore/Constants.cpp
+++ b/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/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp
index 1fa83eb..f85dbe7 100644
--- a/lib/VMCore/Core.cpp
+++ b/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)));
}