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/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 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);
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);
36 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
38 GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/,
39 GlobalValue::InternalLinkage, nullptr,"switch.bas");
42 std::vector<Constant *> Init;
43 Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB);
44 Init.push_back(SwitchCase1BA);
46 Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB);
47 Init.push_back(SwitchCase2BA);
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);
54 GV->setInitializer(ConstantArray::get(AT, Init));
57 virtual void TearDown() { M.reset(); }
60 std::unique_ptr<Module> M;
64 BasicBlock *SwitchCase1BB;
65 BasicBlock *SwitchCase2BB;
69 TEST_F(LinkModuleTest, BlockAddress) {
70 IRBuilder<> Builder(EntryBB);
72 std::vector<Value *> GEPIndices;
73 GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
74 GEPIndices.push_back(F->arg_begin());
76 Value *GEP = Builder.CreateGEP(GV, GEPIndices, "switch.gep");
77 Value *Load = Builder.CreateLoad(GEP, "switch.load");
79 Builder.CreateRet(Load);
81 Builder.SetInsertPoint(SwitchCase1BB);
82 Builder.CreateBr(ExitBB);
84 Builder.SetInsertPoint(SwitchCase2BB);
85 Builder.CreateBr(ExitBB);
87 Builder.SetInsertPoint(ExitBB);
88 Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
90 Module *LinkedModule = new Module("MyModuleLinked", Ctx);
91 Linker::LinkModules(LinkedModule, M.get(), Linker::PreserveSource, nullptr);
93 // Delete the original module.
96 // Check that the global "@switch.bas" is well-formed.
97 const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
98 const Constant *Init = LinkedGV->getInitializer();
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*)]
105 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
106 EXPECT_EQ(AT, Init->getType());
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"));
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"));
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*/);
130 F = Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
131 F->setCallingConv(CallingConv::C);
133 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
134 IRBuilder<> Builder(BB);
135 Builder.CreateRetVoid();
137 StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
140 new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
141 GlobalValue::InternalLinkage, nullptr, "g");
143 GV->setInitializer(ConstantStruct::get(STy, F));
145 Module *EmptyM = new Module("EmptyModule1", Ctx);
146 Linker::LinkModules(EmptyM, InternalM, Linker::PreserveSource, nullptr);
149 EmptyM = new Module("EmptyModule2", Ctx);
150 Linker::LinkModules(InternalM, EmptyM, Linker::PreserveSource, nullptr);
156 } // end anonymous namespace