fbd03639fa704bf1e7d8ce20a5dd2b1326643ffe
[oota-llvm.git] / unittests / Linker / LinkModulesTest.cpp
1 //===- llvm/unittest/Linker/LinkModulesTest.cpp - IRBuilder tests ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
19
20 using namespace llvm;
21
22 namespace {
23
24 class LinkModuleTest : public testing::Test {
25 protected:
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);
32
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);
37
38     AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
39
40     GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/,
41                             GlobalValue::InternalLinkage, nullptr,"switch.bas");
42
43     // Global Initializer
44     std::vector<Constant *> Init;
45     Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB);
46     Init.push_back(SwitchCase1BA);
47
48     Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB);
49     Init.push_back(SwitchCase2BA);
50
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);
55
56     GV->setInitializer(ConstantArray::get(AT, Init));
57   }
58
59   virtual void TearDown() { M.reset(); }
60
61   LLVMContext Ctx;
62   std::unique_ptr<Module> M;
63   Function *F;
64   ArrayType *AT;
65   GlobalVariable *GV;
66   BasicBlock *EntryBB;
67   BasicBlock *SwitchCase1BB;
68   BasicBlock *SwitchCase2BB;
69   BasicBlock *ExitBB;
70 };
71
72 TEST_F(LinkModuleTest, BlockAddress) {
73   IRBuilder<> Builder(EntryBB);
74
75   std::vector<Value *> GEPIndices;
76   GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
77   GEPIndices.push_back(F->arg_begin());
78
79   Value *GEP = Builder.CreateGEP(AT, GV, GEPIndices, "switch.gep");
80   Value *Load = Builder.CreateLoad(GEP, "switch.load");
81
82   Builder.CreateRet(Load);
83
84   Builder.SetInsertPoint(SwitchCase1BB);
85   Builder.CreateBr(ExitBB);
86
87   Builder.SetInsertPoint(SwitchCase2BB);
88   Builder.CreateBr(ExitBB);
89
90   Builder.SetInsertPoint(ExitBB);
91   Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
92
93   Module *LinkedModule = new Module("MyModuleLinked", Ctx);
94   Linker::LinkModules(LinkedModule, M.get());
95
96   // Delete the original module.
97   M.reset();
98
99   // Check that the global "@switch.bas" is well-formed.
100   const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
101   const Constant *Init = LinkedGV->getInitializer();
102
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*)]
107
108   ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
109   EXPECT_EQ(AT, Init->getType());
110
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"));
117
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"));
124
125   delete LinkedModule;
126 }
127
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*/);
132
133   Function *F =
134       Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
135   F->setCallingConv(CallingConv::C);
136
137   BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
138   IRBuilder<> Builder(BB);
139   Builder.CreateRetVoid();
140
141   StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
142
143   GlobalVariable *GV =
144       new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
145                          GlobalValue::InternalLinkage, nullptr, "g");
146
147   GV->setInitializer(ConstantStruct::get(STy, F));
148   return InternalM;
149 }
150
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());
155 }
156
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());
161 }
162
163 TEST_F(LinkModuleTest, TypeMerge) {
164   LLVMContext C;
165   SMDiagnostic Err;
166
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);
170
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);
174
175   Linker::LinkModules(M1.get(), M2.get(), [](const llvm::DiagnosticInfo &){});
176
177   EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
178             M1->getNamedGlobal("t2")->getType());
179 }
180
181 } // end anonymous namespace