summaryrefslogtreecommitdiff
path: root/unittests/IR/ConstantsTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'unittests/IR/ConstantsTest.cpp')
-rw-r--r--unittests/IR/ConstantsTest.cpp73
1 files changed, 71 insertions, 2 deletions
diff --git a/unittests/IR/ConstantsTest.cpp b/unittests/IR/ConstantsTest.cpp
index 2df008a78ccd..7471584097dd 100644
--- a/unittests/IR/ConstantsTest.cpp
+++ b/unittests/IR/ConstantsTest.cpp
@@ -15,6 +15,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
+#include "llvm-c/Core.h"
#include "gtest/gtest.h"
namespace llvm {
@@ -348,7 +349,7 @@ TEST(ConstantsTest, GEPReplaceWithConstant) {
std::unique_ptr<Module> M(new Module("MyModule", Context));
Type *IntTy = Type::getInt32Ty(Context);
- auto *PtrTy = PointerType::get(IntTy, 0);
+ Type *PtrTy = PointerType::get(IntTy, 0);
auto *C1 = ConstantInt::get(IntTy, 1);
auto *Placeholder = new GlobalVariable(
*M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
@@ -361,7 +362,7 @@ TEST(ConstantsTest, GEPReplaceWithConstant) {
auto *Global = new GlobalVariable(*M, PtrTy, false,
GlobalValue::ExternalLinkage, nullptr);
- auto *Alias = GlobalAlias::create(PtrTy, GlobalValue::ExternalLinkage,
+ auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
"alias", Global, M.get());
Placeholder->replaceAllUsesWith(Alias);
ASSERT_EQ(GEP, Ref->getInitializer());
@@ -382,5 +383,73 @@ TEST(ConstantsTest, AliasCAPI) {
ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
}
+static std::string getNameOfType(Type *T) {
+ std::string S;
+ raw_string_ostream RSOS(S);
+ T->print(RSOS);
+ return S;
+}
+
+TEST(ConstantsTest, BuildConstantDataArrays) {
+ LLVMContext Context;
+ std::unique_ptr<Module> M(new Module("MyModule", Context));
+
+ for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
+ Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
+ ArrayType *ArrayTy = ArrayType::get(T, 2);
+ Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
+ Constant *CDV = ConstantArray::get(ArrayTy, Vals);
+ ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
+ << " T = " << getNameOfType(T);
+ }
+
+ for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
+ Type::getDoubleTy(Context)}) {
+ ArrayType *ArrayTy = ArrayType::get(T, 2);
+ Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
+ Constant *CDV = ConstantArray::get(ArrayTy, Vals);
+ ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
+ << " T = " << getNameOfType(T);
+ }
+}
+
+TEST(ConstantsTest, BuildConstantDataVectors) {
+ LLVMContext Context;
+ std::unique_ptr<Module> M(new Module("MyModule", Context));
+
+ for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
+ Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
+ Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
+ Constant *CDV = ConstantVector::get(Vals);
+ ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
+ << " T = " << getNameOfType(T);
+ }
+
+ for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
+ Type::getDoubleTy(Context)}) {
+ Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
+ Constant *CDV = ConstantVector::get(Vals);
+ ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
+ << " T = " << getNameOfType(T);
+ }
+}
+
+TEST(ConstantsTest, BitcastToGEP) {
+ LLVMContext Context;
+ std::unique_ptr<Module> M(new Module("MyModule", Context));
+
+ auto *i32 = Type::getInt32Ty(Context);
+ auto *U = StructType::create(Context, "Unsized");
+ Type *EltTys[] = {i32, U};
+ auto *S = StructType::create(EltTys);
+
+ auto *G = new GlobalVariable(*M, S, false,
+ GlobalValue::ExternalLinkage, nullptr);
+ auto *PtrTy = PointerType::get(i32, 0);
+ auto *C = ConstantExpr::getBitCast(G, PtrTy);
+ ASSERT_EQ(dyn_cast<ConstantExpr>(C)->getOpcode(),
+ Instruction::BitCast);
+}
+
} // end anonymous namespace
} // end namespace llvm