summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/IR/ConstantsTest.cpp
diff options
context:
space:
mode:
authorMichael Platings <michael.platings@arm.com>2019-03-07 09:15:23 +0000
committerMichael Platings <michael.platings@arm.com>2019-03-07 09:15:23 +0000
commitfd4156ed4d30828fbcca7b42618dde0550c9b674 (patch)
treefdc8b02c65f61b60fcdd2b7702a1e77badce6b9a /llvm/unittests/IR/ConstantsTest.cpp
parentb0f764c73732958745f13e5310131d0e7c3fa400 (diff)
downloadbcm5719-llvm-fd4156ed4d30828fbcca7b42618dde0550c9b674.tar.gz
bcm5719-llvm-fd4156ed4d30828fbcca7b42618dde0550c9b674.zip
[IR][ARM] Add function pointer alignment to datalayout
Use this feature to fix a bug on ARM where 4 byte alignment is incorrectly assumed. Differential Revision: https://reviews.llvm.org/D57335 llvm-svn: 355585
Diffstat (limited to 'llvm/unittests/IR/ConstantsTest.cpp')
-rw-r--r--llvm/unittests/IR/ConstantsTest.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp
index 6b26b38e35e..c0fe18b1182 100644
--- a/llvm/unittests/IR/ConstantsTest.cpp
+++ b/llvm/unittests/IR/ConstantsTest.cpp
@@ -475,5 +475,105 @@ TEST(ConstantsTest, BitcastToGEP) {
ASSERT_EQ(cast<ConstantExpr>(C)->getOpcode(), Instruction::BitCast);
}
+bool foldFuncPtrAndConstToNull(LLVMContext &Context, Module *TheModule,
+ uint64_t AndValue, unsigned FunctionAlign = 0) {
+ Type *VoidType(Type::getVoidTy(Context));
+ FunctionType *FuncType(FunctionType::get(VoidType, false));
+ Function *Func(Function::Create(
+ FuncType, GlobalValue::ExternalLinkage, "", TheModule));
+
+ if (FunctionAlign) Func->setAlignment(FunctionAlign);
+
+ IntegerType *ConstantIntType(Type::getInt32Ty(Context));
+ ConstantInt *TheConstant(ConstantInt::get(ConstantIntType, AndValue));
+
+ Constant *TheConstantExpr(
+ ConstantExpr::getPtrToInt(Func, ConstantIntType));
+
+ return ConstantExpr::get(Instruction::And, TheConstantExpr,
+ TheConstant)->isNullValue();
+}
+
+TEST(ConstantsTest, FoldFunctionPtrAlignUnknownAnd2) {
+ LLVMContext Context;
+ Module TheModule("TestModule", Context);
+ // When the DataLayout doesn't specify a function pointer alignment we
+ // assume in this case that it is 4 byte aligned. This is a bug but we can't
+ // fix it directly because it causes a code size regression on X86.
+ // FIXME: This test should be changed once existing targets have
+ // appropriate defaults. See associated FIXME in ConstantFoldBinaryInstruction
+ ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, 2));
+}
+
+TEST(ConstantsTest, DontFoldFunctionPtrAlignUnknownAnd4) {
+ LLVMContext Context;
+ Module TheModule("TestModule", Context);
+ ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 4));
+}
+
+TEST(ConstantsTest, FoldFunctionPtrAlign4) {
+ LLVMContext Context;
+ Module TheModule("TestModule", Context);
+ const char* AlignmentStrings[] = { "Fi32", "Fn32" };
+
+ for (unsigned AndValue = 1; AndValue <= 2; ++AndValue) {
+ for (const char *AlignmentString : AlignmentStrings) {
+ TheModule.setDataLayout(AlignmentString);
+ ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, AndValue));
+ }
+ }
+}
+
+TEST(ConstantsTest, DontFoldFunctionPtrAlign1) {
+ LLVMContext Context;
+ Module TheModule("TestModule", Context);
+ const char* AlignmentStrings[] = { "Fi8", "Fn8" };
+
+ for (const char* AlignmentString : AlignmentStrings) {
+ TheModule.setDataLayout(AlignmentString);
+ ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2));
+ }
+}
+
+TEST(ConstantsTest, FoldFunctionAlign4PtrAlignMultiple) {
+ LLVMContext Context;
+ Module TheModule("TestModule", Context);
+ TheModule.setDataLayout("Fn8");
+ ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, 4));
+}
+
+TEST(ConstantsTest, DontFoldFunctionAlign4PtrAlignIndependent) {
+ LLVMContext Context;
+ Module TheModule("TestModule", Context);
+ TheModule.setDataLayout("Fi8");
+ ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, 4));
+}
+
+TEST(ConstantsTest, DontFoldFunctionPtrIfNoModule) {
+ LLVMContext Context;
+ // Even though the function is explicitly 4 byte aligned, in the absence of a
+ // DataLayout we can't assume that the function pointer is aligned.
+ ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, nullptr, 2, 4));
+}
+
+TEST(ConstantsTest, FoldGlobalVariablePtr) {
+ LLVMContext Context;
+
+
+ IntegerType *IntType(Type::getInt32Ty(Context));
+
+ GlobalVariable Global(IntType, true, GlobalValue::ExternalLinkage);
+
+ Global.setAlignment(4);
+
+ ConstantInt *TheConstant(ConstantInt::get(IntType, 2));
+
+ Constant *TheConstantExpr(
+ ConstantExpr::getPtrToInt(&Global, IntType));
+
+ ASSERT_TRUE(ConstantExpr::get( \
+ Instruction::And, TheConstantExpr, TheConstant)->isNullValue());
+}
+
} // end anonymous namespace
} // end namespace llvm
OpenPOWER on IntegriCloud