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/IR/BasicBlock.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/IRBuilder.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Linker/Linker.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 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;
67 BasicBlock *SwitchCase1BB;
68 BasicBlock *SwitchCase2BB;
72 TEST_F(LinkModuleTest, BlockAddress) {
73 IRBuilder<> Builder(EntryBB);
75 std::vector<Value *> GEPIndices;
76 GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
77 GEPIndices.push_back(F->arg_begin());
79 Value *GEP = Builder.CreateGEP(AT, GV, GEPIndices, "switch.gep");
80 Value *Load = Builder.CreateLoad(GEP, "switch.load");
82 Builder.CreateRet(Load);
84 Builder.SetInsertPoint(SwitchCase1BB);
85 Builder.CreateBr(ExitBB);
87 Builder.SetInsertPoint(SwitchCase2BB);
88 Builder.CreateBr(ExitBB);
90 Builder.SetInsertPoint(ExitBB);
91 Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
93 Module *LinkedModule = new Module("MyModuleLinked", Ctx);
94 Linker::LinkModules(LinkedModule, M.get());
96 // Delete the original module.
99 // Check that the global "@switch.bas" is well-formed.
100 const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
101 const Constant *Init = LinkedGV->getInitializer();
103 // @switch.bas = internal global [3 x i8*]
104 // [i8* blockaddress(@ba_func, %switch.case.1),
105 // i8* blockaddress(@ba_func, %switch.case.2),
106 // i8* inttoptr (i32 1 to i8*)]
108 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
109 EXPECT_EQ(AT, Init->getType());
111 Value *Elem = Init->getOperand(0);
112 ASSERT_TRUE(isa<BlockAddress>(Elem));
113 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
114 LinkedModule->getFunction("ba_func"));
115 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
116 LinkedModule->getFunction("ba_func"));
118 Elem = Init->getOperand(1);
119 ASSERT_TRUE(isa<BlockAddress>(Elem));
120 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
121 LinkedModule->getFunction("ba_func"));
122 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
123 LinkedModule->getFunction("ba_func"));
128 static Module *getInternal(LLVMContext &Ctx) {
129 Module *InternalM = new Module("InternalModule", Ctx);
130 FunctionType *FTy = FunctionType::get(
131 Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
134 Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
135 F->setCallingConv(CallingConv::C);
137 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
138 IRBuilder<> Builder(BB);
139 Builder.CreateRetVoid();
141 StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
144 new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
145 GlobalValue::InternalLinkage, nullptr, "g");
147 GV->setInitializer(ConstantStruct::get(STy, F));
151 TEST_F(LinkModuleTest, EmptyModule) {
152 std::unique_ptr<Module> InternalM(getInternal(Ctx));
153 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
154 Linker::LinkModules(EmptyM.get(), InternalM.get());
157 TEST_F(LinkModuleTest, EmptyModule2) {
158 std::unique_ptr<Module> InternalM(getInternal(Ctx));
159 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
160 Linker::LinkModules(InternalM.get(), EmptyM.get());
163 TEST_F(LinkModuleTest, TypeMerge) {
167 const char *M1Str = "%t = type {i32}\n"
168 "@t1 = weak global %t zeroinitializer\n";
169 std::unique_ptr<Module> M1 = parseAssemblyString(M1Str, Err, C);
171 const char *M2Str = "%t = type {i32}\n"
172 "@t2 = weak global %t zeroinitializer\n";
173 std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C);
175 Linker::LinkModules(M1.get(), M2.get(), [](const llvm::DiagnosticInfo &){});
177 EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
178 M1->getNamedGlobal("t2")->getType());
181 } // end anonymous namespace