540849ec6a86a310ea9cb1e43ee9a55861f9a54f
[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/Linker/Linker.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 "gtest/gtest.h"
17
18 using namespace llvm;
19
20 namespace {
21
22 class LinkModuleTest : public testing::Test {
23 protected:
24   virtual void SetUp() {
25     M.reset(new Module("MyModule", Ctx));
26     FunctionType *FTy = FunctionType::get(
27         Type::getInt8PtrTy(Ctx), Type::getInt32Ty(Ctx), false /*=isVarArg*/);
28     F = Function::Create(FTy, Function::ExternalLinkage, "ba_func", M.get());
29     F->setCallingConv(CallingConv::C);
30
31     EntryBB = BasicBlock::Create(Ctx, "entry", F);
32     SwitchCase1BB = BasicBlock::Create(Ctx, "switch.case.1", F);
33     SwitchCase2BB = BasicBlock::Create(Ctx, "switch.case.2", F);
34     ExitBB = BasicBlock::Create(Ctx, "exit", F);
35
36     ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
37
38     GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/,
39                             GlobalValue::InternalLinkage, nullptr,"switch.bas");
40
41     // Global Initializer
42     std::vector<Constant *> Init;
43     Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB);
44     Init.push_back(SwitchCase1BA);
45
46     Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB);
47     Init.push_back(SwitchCase2BA);
48
49     ConstantInt *One = ConstantInt::get(Type::getInt32Ty(Ctx), 1);
50     Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr, One,
51                                              Type::getInt8PtrTy(Ctx));
52     Init.push_back(OnePtr);
53
54     GV->setInitializer(ConstantArray::get(AT, Init));
55   }
56
57   virtual void TearDown() { M.reset(); }
58
59   LLVMContext Ctx;
60   std::unique_ptr<Module> M;
61   Function *F;
62   GlobalVariable *GV;
63   BasicBlock *EntryBB;
64   BasicBlock *SwitchCase1BB;
65   BasicBlock *SwitchCase2BB;
66   BasicBlock *ExitBB;
67 };
68
69 TEST_F(LinkModuleTest, BlockAddress) {
70   IRBuilder<> Builder(EntryBB);
71
72   std::vector<Value *> GEPIndices;
73   GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
74   GEPIndices.push_back(F->arg_begin());
75
76   Value *GEP = Builder.CreateGEP(GV, GEPIndices, "switch.gep");
77   Value *Load = Builder.CreateLoad(GEP, "switch.load");
78
79   Builder.CreateRet(Load);
80
81   Builder.SetInsertPoint(SwitchCase1BB);
82   Builder.CreateBr(ExitBB);
83
84   Builder.SetInsertPoint(SwitchCase2BB);
85   Builder.CreateBr(ExitBB);
86
87   Builder.SetInsertPoint(ExitBB);
88   Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
89
90   Module *LinkedModule = new Module("MyModuleLinked", Ctx);
91   Linker::LinkModules(LinkedModule, M.get(), Linker::PreserveSource);
92
93   // Delete the original module.
94   M.reset();
95
96   // Check that the global "@switch.bas" is well-formed.
97   const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
98   const Constant *Init = LinkedGV->getInitializer();
99
100   // @switch.bas = internal global [3 x i8*]
101   //   [i8* blockaddress(@ba_func, %switch.case.1),
102   //    i8* blockaddress(@ba_func, %switch.case.2),
103   //    i8* inttoptr (i32 1 to i8*)]
104
105   ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
106   EXPECT_EQ(AT, Init->getType());
107
108   Value *Elem = Init->getOperand(0);
109   ASSERT_TRUE(isa<BlockAddress>(Elem));
110   EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
111             LinkedModule->getFunction("ba_func"));
112   EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
113             LinkedModule->getFunction("ba_func"));
114
115   Elem = Init->getOperand(1);
116   ASSERT_TRUE(isa<BlockAddress>(Elem));
117   EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
118             LinkedModule->getFunction("ba_func"));
119   EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
120             LinkedModule->getFunction("ba_func"));
121
122   delete LinkedModule;
123 }
124
125 TEST_F(LinkModuleTest, EmptyModule) {
126   Module *InternalM = new Module("InternalModule", Ctx);
127   FunctionType *FTy = FunctionType::get(
128       Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
129
130   F = Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
131   F->setCallingConv(CallingConv::C);
132
133   BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
134   IRBuilder<> Builder(BB);
135   Builder.CreateRetVoid();
136
137   StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
138
139   GlobalVariable *GV =
140       new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
141                          GlobalValue::InternalLinkage, nullptr, "g");
142
143   GV->setInitializer(ConstantStruct::get(STy, F));
144
145   Module *EmptyM = new Module("EmptyModule1", Ctx);
146   Linker::LinkModules(EmptyM, InternalM, Linker::PreserveSource);
147
148   delete EmptyM;
149   EmptyM = new Module("EmptyModule2", Ctx);
150   Linker::LinkModules(InternalM, EmptyM, Linker::PreserveSource);
151
152   delete EmptyM;
153   delete InternalM;
154 }
155
156 } // end anonymous namespace