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/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"
22 class LinkModuleTest : public testing::Test {
24 virtual void SetUp() {
25 LLVMContext &Ctx = getGlobalContext();
26 M.reset(new Module("MyModule", Ctx));
27 FunctionType *FTy = FunctionType::get(Type::getInt8PtrTy(Ctx),
28 Type::getInt32Ty(Ctx),
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,
46 std::vector<Constant*> Init;
47 Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB);
48 Init.push_back(SwitchCase1BA);
50 Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB);
51 Init.push_back(SwitchCase2BA);
53 ConstantInt *One = ConstantInt::get(Type::getInt32Ty(Ctx), 1);
54 Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr,
55 One, Type::getInt8PtrTy(Ctx));
56 Init.push_back(OnePtr);
58 GV->setInitializer(ConstantArray::get(AT, Init));
61 virtual void TearDown() {
69 BasicBlock *SwitchCase1BB;
70 BasicBlock *SwitchCase2BB;
74 TEST_F(LinkModuleTest, BlockAddress) {
75 LLVMContext &Ctx = getGlobalContext();
76 IRBuilder<> Builder(EntryBB);
78 std::vector<Value*> GEPIndices;
79 GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
80 GEPIndices.push_back(F->arg_begin());
82 Value *GEP = Builder.CreateGEP(GV, GEPIndices, "switch.gep");
83 Value *Load = Builder.CreateLoad(GEP, "switch.load");
85 Builder.CreateRet(Load);
87 Builder.SetInsertPoint(SwitchCase1BB);
88 Builder.CreateBr(ExitBB);
90 Builder.SetInsertPoint(SwitchCase2BB);
91 Builder.CreateBr(ExitBB);
93 Builder.SetInsertPoint(ExitBB);
94 Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
96 Module *LinkedModule = new Module("MyModuleLinked", getGlobalContext());
97 Linker::LinkModules(LinkedModule, M.get(), Linker::PreserveSource, 0);
99 // Delete the original module.
102 // Check that the global "@switch.bas" is well-formed.
103 const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
104 const Constant *Init = LinkedGV->getInitializer();
106 // @switch.bas = internal global [3 x i8*]
107 // [i8* blockaddress(@ba_func, %switch.case.1),
108 // i8* blockaddress(@ba_func, %switch.case.2),
109 // i8* inttoptr (i32 1 to i8*)]
111 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
112 EXPECT_EQ(AT, Init->getType());
114 Value *Elem = Init->getOperand(0);
115 ASSERT_TRUE(isa<BlockAddress>(Elem));
116 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
117 LinkedModule->getFunction("ba_func"));
118 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
119 LinkedModule->getFunction("ba_func"));
121 Elem = Init->getOperand(1);
122 ASSERT_TRUE(isa<BlockAddress>(Elem));
123 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
124 LinkedModule->getFunction("ba_func"));
125 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
126 LinkedModule->getFunction("ba_func"));
131 TEST_F(LinkModuleTest, EmptyModule) {
132 LLVMContext &Ctx = getGlobalContext();
133 Module *InternalM = new Module("InternalModule", Ctx);
134 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
135 Type::getInt8PtrTy(Ctx),
136 false /*=isVarArgs*/);
138 F = Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
139 F->setCallingConv(CallingConv::C);
141 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
142 IRBuilder<> Builder(BB);
143 Builder.CreateRetVoid();
145 StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
148 new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
149 GlobalValue::InternalLinkage, 0, "g");
151 GV->setInitializer(ConstantStruct::get(STy, F));
154 Module *EmptyM = new Module("EmptyModule1", Ctx);
155 Linker::LinkModules(EmptyM, InternalM, Linker::PreserveSource, 0);
158 EmptyM = new Module("EmptyModule2", Ctx);
159 Linker::LinkModules(InternalM, EmptyM, Linker::PreserveSource, 0);
165 } // end anonymous namespace