1 //===- llvm/unittest/Linker/LinkModulesTest.cpp - IRBuilder tests ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/Linker/Linker.h"
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "gtest/gtest.h"
24 class LinkModuleTest : public testing::Test {
26 virtual void SetUp() {
27 M.reset(new Module("MyModule", Ctx));
28 FunctionType *FTy = FunctionType::get(
29 Type::getInt8PtrTy(Ctx), Type::getInt32Ty(Ctx), false /*=isVarArg*/);
30 F = Function::Create(FTy, Function::ExternalLinkage, "ba_func", M.get());
31 F->setCallingConv(CallingConv::C);
33 EntryBB = BasicBlock::Create(Ctx, "entry", F);
34 SwitchCase1BB = BasicBlock::Create(Ctx, "switch.case.1", F);
35 SwitchCase2BB = BasicBlock::Create(Ctx, "switch.case.2", F);
36 ExitBB = BasicBlock::Create(Ctx, "exit", F);
38 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
40 GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/,
41 GlobalValue::InternalLinkage, nullptr,"switch.bas");
44 std::vector<Constant *> Init;
45 Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB);
46 Init.push_back(SwitchCase1BA);
48 Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB);
49 Init.push_back(SwitchCase2BA);
51 ConstantInt *One = ConstantInt::get(Type::getInt32Ty(Ctx), 1);
52 Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr, One,
53 Type::getInt8PtrTy(Ctx));
54 Init.push_back(OnePtr);
56 GV->setInitializer(ConstantArray::get(AT, Init));
59 virtual void TearDown() { M.reset(); }
62 std::unique_ptr<Module> M;
66 BasicBlock *SwitchCase1BB;
67 BasicBlock *SwitchCase2BB;
71 TEST_F(LinkModuleTest, BlockAddress) {
72 IRBuilder<> Builder(EntryBB);
74 std::vector<Value *> GEPIndices;
75 GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
76 GEPIndices.push_back(F->arg_begin());
78 Value *GEP = Builder.CreateGEP(GV, GEPIndices, "switch.gep");
79 Value *Load = Builder.CreateLoad(GEP, "switch.load");
81 Builder.CreateRet(Load);
83 Builder.SetInsertPoint(SwitchCase1BB);
84 Builder.CreateBr(ExitBB);
86 Builder.SetInsertPoint(SwitchCase2BB);
87 Builder.CreateBr(ExitBB);
89 Builder.SetInsertPoint(ExitBB);
90 Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
92 Module *LinkedModule = new Module("MyModuleLinked", Ctx);
93 Linker::LinkModules(LinkedModule, M.get());
95 // Delete the original module.
98 // Check that the global "@switch.bas" is well-formed.
99 const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
100 const Constant *Init = LinkedGV->getInitializer();
102 // @switch.bas = internal global [3 x i8*]
103 // [i8* blockaddress(@ba_func, %switch.case.1),
104 // i8* blockaddress(@ba_func, %switch.case.2),
105 // i8* inttoptr (i32 1 to i8*)]
107 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
108 EXPECT_EQ(AT, Init->getType());
110 Value *Elem = Init->getOperand(0);
111 ASSERT_TRUE(isa<BlockAddress>(Elem));
112 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
113 LinkedModule->getFunction("ba_func"));
114 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
115 LinkedModule->getFunction("ba_func"));
117 Elem = Init->getOperand(1);
118 ASSERT_TRUE(isa<BlockAddress>(Elem));
119 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
120 LinkedModule->getFunction("ba_func"));
121 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
122 LinkedModule->getFunction("ba_func"));
127 static Module *getInternal(LLVMContext &Ctx) {
128 Module *InternalM = new Module("InternalModule", Ctx);
129 FunctionType *FTy = FunctionType::get(
130 Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
133 Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
134 F->setCallingConv(CallingConv::C);
136 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
137 IRBuilder<> Builder(BB);
138 Builder.CreateRetVoid();
140 StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
143 new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
144 GlobalValue::InternalLinkage, nullptr, "g");
146 GV->setInitializer(ConstantStruct::get(STy, F));
150 TEST_F(LinkModuleTest, EmptyModule) {
151 std::unique_ptr<Module> InternalM(getInternal(Ctx));
152 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
153 Linker::LinkModules(EmptyM.get(), InternalM.get());
156 TEST_F(LinkModuleTest, EmptyModule2) {
157 std::unique_ptr<Module> InternalM(getInternal(Ctx));
158 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
159 Linker::LinkModules(InternalM.get(), EmptyM.get());
162 TEST_F(LinkModuleTest, TypeMerge) {
166 const char *M1Str = "%t = type {i32}\n"
167 "@t1 = weak global %t zeroinitializer\n";
168 std::unique_ptr<Module> M1 = parseAssemblyString(M1Str, Err, C);
170 const char *M2Str = "%t = type {i32}\n"
171 "@t2 = weak global %t zeroinitializer\n";
172 std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C);
174 Linker::LinkModules(M1.get(), M2.get(), [](const llvm::DiagnosticInfo &){});
176 EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
177 M1->getNamedGlobal("t2")->getType());
180 } // end anonymous namespace