summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/IR
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/unittests/IR')
-rw-r--r--llvm/unittests/IR/CMakeLists.txt1
-rw-r--r--llvm/unittests/IR/ConstantsTest.cpp100
-rw-r--r--llvm/unittests/IR/DataLayoutTest.cpp47
-rw-r--r--llvm/unittests/IR/FunctionTest.cpp25
4 files changed, 173 insertions, 0 deletions
diff --git a/llvm/unittests/IR/CMakeLists.txt b/llvm/unittests/IR/CMakeLists.txt
index a823407169f..c3cdf90c0ef 100644
--- a/llvm/unittests/IR/CMakeLists.txt
+++ b/llvm/unittests/IR/CMakeLists.txt
@@ -13,6 +13,7 @@ add_llvm_unittest(IRTests
CFGBuilder.cpp
ConstantRangeTest.cpp
ConstantsTest.cpp
+ DataLayoutTest.cpp
DebugInfoTest.cpp
DebugTypeODRUniquingTest.cpp
DominatorTreeTest.cpp
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
diff --git a/llvm/unittests/IR/DataLayoutTest.cpp b/llvm/unittests/IR/DataLayoutTest.cpp
new file mode 100644
index 00000000000..e7ed70b7de5
--- /dev/null
+++ b/llvm/unittests/IR/DataLayoutTest.cpp
@@ -0,0 +1,47 @@
+//===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/DataLayout.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(DataLayoutTest, FunctionPtrAlign) {
+ EXPECT_EQ(0U, DataLayout("").getFunctionPtrAlign());
+ EXPECT_EQ(1U, DataLayout("Fi8").getFunctionPtrAlign());
+ EXPECT_EQ(2U, DataLayout("Fi16").getFunctionPtrAlign());
+ EXPECT_EQ(4U, DataLayout("Fi32").getFunctionPtrAlign());
+ EXPECT_EQ(8U, DataLayout("Fi64").getFunctionPtrAlign());
+ EXPECT_EQ(1U, DataLayout("Fn8").getFunctionPtrAlign());
+ EXPECT_EQ(2U, DataLayout("Fn16").getFunctionPtrAlign());
+ EXPECT_EQ(4U, DataLayout("Fn32").getFunctionPtrAlign());
+ EXPECT_EQ(8U, DataLayout("Fn64").getFunctionPtrAlign());
+ EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \
+ DataLayout("").getFunctionPtrAlignType());
+ EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \
+ DataLayout("Fi8").getFunctionPtrAlignType());
+ EXPECT_EQ(DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign, \
+ DataLayout("Fn8").getFunctionPtrAlignType());
+ EXPECT_EQ(DataLayout("Fi8"), DataLayout("Fi8"));
+ EXPECT_NE(DataLayout("Fi8"), DataLayout("Fi16"));
+ EXPECT_NE(DataLayout("Fi8"), DataLayout("Fn8"));
+
+ DataLayout a(""), b("Fi8"), c("Fn8");
+ EXPECT_NE(a, b);
+ EXPECT_NE(a, c);
+ EXPECT_NE(b, c);
+
+ a = b;
+ EXPECT_EQ(a, b);
+ a = c;
+ EXPECT_EQ(a, c);
+}
+
+} // anonymous namespace
diff --git a/llvm/unittests/IR/FunctionTest.cpp b/llvm/unittests/IR/FunctionTest.cpp
index 10a359ff64b..c68b064afe3 100644
--- a/llvm/unittests/IR/FunctionTest.cpp
+++ b/llvm/unittests/IR/FunctionTest.cpp
@@ -129,4 +129,29 @@ TEST(FunctionTest, setSection) {
EXPECT_TRUE(F->hasSection());
}
+TEST(FunctionTest, GetPointerAlignment) {
+ LLVMContext Context;
+ Type *VoidType(Type::getVoidTy(Context));
+ FunctionType *FuncType(FunctionType::get(VoidType, false));
+ Function *Func = Function::Create(
+ FuncType, GlobalValue::ExternalLinkage);
+ EXPECT_EQ(0U, Func->getPointerAlignment(DataLayout("")));
+ EXPECT_EQ(1U, Func->getPointerAlignment(DataLayout("Fi8")));
+ EXPECT_EQ(1U, Func->getPointerAlignment(DataLayout("Fn8")));
+ EXPECT_EQ(2U, Func->getPointerAlignment(DataLayout("Fi16")));
+ EXPECT_EQ(2U, Func->getPointerAlignment(DataLayout("Fn16")));
+ EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fi32")));
+ EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fn32")));
+
+ Func->setAlignment(4U);
+
+ EXPECT_EQ(0U, Func->getPointerAlignment(DataLayout("")));
+ EXPECT_EQ(1U, Func->getPointerAlignment(DataLayout("Fi8")));
+ EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fn8")));
+ EXPECT_EQ(2U, Func->getPointerAlignment(DataLayout("Fi16")));
+ EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fn16")));
+ EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fi32")));
+ EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fn32")));
+}
+
} // end namespace
OpenPOWER on IntegriCloud