[cleanup] Re-sort all the #include lines in LLVM using
[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     ArrayType *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   GlobalVariable *GV;
65   BasicBlock *EntryBB;
66   BasicBlock *SwitchCase1BB;
67   BasicBlock *SwitchCase2BB;
68   BasicBlock *ExitBB;
69 };
70
71 TEST_F(LinkModuleTest, BlockAddress) {
72   IRBuilder<> Builder(EntryBB);
73
74   std::vector<Value *> GEPIndices;
75   GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
76   GEPIndices.push_back(F->arg_begin());
77
78   Value *GEP = Builder.CreateGEP(GV, GEPIndices, "switch.gep");
79   Value *Load = Builder.CreateLoad(GEP, "switch.load");
80
81   Builder.CreateRet(Load);
82
83   Builder.SetInsertPoint(SwitchCase1BB);
84   Builder.CreateBr(ExitBB);
85
86   Builder.SetInsertPoint(SwitchCase2BB);
87   Builder.CreateBr(ExitBB);
88
89   Builder.SetInsertPoint(ExitBB);
90   Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
91
92   Module *LinkedModule = new Module("MyModuleLinked", Ctx);
93   Linker::LinkModules(LinkedModule, M.get());
94
95   // Delete the original module.
96   M.reset();
97
98   // Check that the global "@switch.bas" is well-formed.
99   const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
100   const Constant *Init = LinkedGV->getInitializer();
101
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*)]
106
107   ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
108   EXPECT_EQ(AT, Init->getType());
109
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"));
116
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"));
123
124   delete LinkedModule;
125 }
126
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*/);
131
132   Function *F =
133       Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
134   F->setCallingConv(CallingConv::C);
135
136   BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
137   IRBuilder<> Builder(BB);
138   Builder.CreateRetVoid();
139
140   StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
141
142   GlobalVariable *GV =
143       new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
144                          GlobalValue::InternalLinkage, nullptr, "g");
145
146   GV->setInitializer(ConstantStruct::get(STy, F));
147   return InternalM;
148 }
149
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());
154 }
155
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());
160 }
161
162 TEST_F(LinkModuleTest, TypeMerge) {
163   LLVMContext C;
164   SMDiagnostic Err;
165
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);
169
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);
173
174   Linker::LinkModules(M1.get(), M2.get(), [](const llvm::DiagnosticInfo &){});
175
176   EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
177             M1->getNamedGlobal("t2")->getType());
178 }
179
180 } // end anonymous namespace