2014-01-16 14:29:36 +08:00
|
|
|
//===- llvm/unittest/Linker/LinkModulesTest.cpp - IRBuilder tests ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-11-18 04:51:01 +08:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2014-03-06 11:42:23 +08:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2014-01-16 14:29:36 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2014-03-04 18:07:28 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-01-16 14:29:36 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-11-18 04:51:01 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2014-01-16 14:29:36 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class LinkModuleTest : public testing::Test {
|
|
|
|
protected:
|
|
|
|
virtual void SetUp() {
|
|
|
|
M.reset(new Module("MyModule", Ctx));
|
2014-04-29 23:52:46 +08:00
|
|
|
FunctionType *FTy = FunctionType::get(
|
|
|
|
Type::getInt8PtrTy(Ctx), Type::getInt32Ty(Ctx), false /*=isVarArg*/);
|
2014-01-16 14:29:36 +08:00
|
|
|
F = Function::Create(FTy, Function::ExternalLinkage, "ba_func", M.get());
|
|
|
|
F->setCallingConv(CallingConv::C);
|
|
|
|
|
|
|
|
EntryBB = BasicBlock::Create(Ctx, "entry", F);
|
|
|
|
SwitchCase1BB = BasicBlock::Create(Ctx, "switch.case.1", F);
|
|
|
|
SwitchCase2BB = BasicBlock::Create(Ctx, "switch.case.2", F);
|
|
|
|
ExitBB = BasicBlock::Create(Ctx, "exit", F);
|
|
|
|
|
|
|
|
ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
|
|
|
|
|
|
|
|
GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/,
|
2014-06-09 06:29:17 +08:00
|
|
|
GlobalValue::InternalLinkage, nullptr,"switch.bas");
|
2014-01-16 14:29:36 +08:00
|
|
|
|
|
|
|
// Global Initializer
|
2014-04-29 23:52:46 +08:00
|
|
|
std::vector<Constant *> Init;
|
2014-01-16 14:29:36 +08:00
|
|
|
Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB);
|
|
|
|
Init.push_back(SwitchCase1BA);
|
|
|
|
|
|
|
|
Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB);
|
|
|
|
Init.push_back(SwitchCase2BA);
|
|
|
|
|
|
|
|
ConstantInt *One = ConstantInt::get(Type::getInt32Ty(Ctx), 1);
|
2014-04-29 23:52:46 +08:00
|
|
|
Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr, One,
|
|
|
|
Type::getInt8PtrTy(Ctx));
|
2014-01-16 14:29:36 +08:00
|
|
|
Init.push_back(OnePtr);
|
|
|
|
|
|
|
|
GV->setInitializer(ConstantArray::get(AT, Init));
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:52:46 +08:00
|
|
|
virtual void TearDown() { M.reset(); }
|
2014-01-16 14:29:36 +08:00
|
|
|
|
2014-04-29 23:52:27 +08:00
|
|
|
LLVMContext Ctx;
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<Module> M;
|
2014-01-16 14:29:36 +08:00
|
|
|
Function *F;
|
|
|
|
GlobalVariable *GV;
|
|
|
|
BasicBlock *EntryBB;
|
|
|
|
BasicBlock *SwitchCase1BB;
|
|
|
|
BasicBlock *SwitchCase2BB;
|
|
|
|
BasicBlock *ExitBB;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(LinkModuleTest, BlockAddress) {
|
|
|
|
IRBuilder<> Builder(EntryBB);
|
|
|
|
|
2014-04-29 23:52:46 +08:00
|
|
|
std::vector<Value *> GEPIndices;
|
2014-01-16 14:29:36 +08:00
|
|
|
GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
|
|
|
|
GEPIndices.push_back(F->arg_begin());
|
|
|
|
|
|
|
|
Value *GEP = Builder.CreateGEP(GV, GEPIndices, "switch.gep");
|
|
|
|
Value *Load = Builder.CreateLoad(GEP, "switch.load");
|
|
|
|
|
|
|
|
Builder.CreateRet(Load);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(SwitchCase1BB);
|
|
|
|
Builder.CreateBr(ExitBB);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(SwitchCase2BB);
|
|
|
|
Builder.CreateBr(ExitBB);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(ExitBB);
|
|
|
|
Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
|
|
|
|
|
2014-04-29 23:52:27 +08:00
|
|
|
Module *LinkedModule = new Module("MyModuleLinked", Ctx);
|
2014-10-28 08:24:16 +08:00
|
|
|
Linker::LinkModules(LinkedModule, M.get());
|
2014-01-16 14:29:36 +08:00
|
|
|
|
|
|
|
// Delete the original module.
|
|
|
|
M.reset();
|
|
|
|
|
|
|
|
// Check that the global "@switch.bas" is well-formed.
|
|
|
|
const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
|
|
|
|
const Constant *Init = LinkedGV->getInitializer();
|
|
|
|
|
|
|
|
// @switch.bas = internal global [3 x i8*]
|
|
|
|
// [i8* blockaddress(@ba_func, %switch.case.1),
|
|
|
|
// i8* blockaddress(@ba_func, %switch.case.2),
|
|
|
|
// i8* inttoptr (i32 1 to i8*)]
|
|
|
|
|
|
|
|
ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
|
|
|
|
EXPECT_EQ(AT, Init->getType());
|
|
|
|
|
|
|
|
Value *Elem = Init->getOperand(0);
|
|
|
|
ASSERT_TRUE(isa<BlockAddress>(Elem));
|
|
|
|
EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
|
|
|
|
LinkedModule->getFunction("ba_func"));
|
|
|
|
EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
|
|
|
|
LinkedModule->getFunction("ba_func"));
|
2014-04-29 23:52:46 +08:00
|
|
|
|
2014-01-16 14:29:36 +08:00
|
|
|
Elem = Init->getOperand(1);
|
|
|
|
ASSERT_TRUE(isa<BlockAddress>(Elem));
|
|
|
|
EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
|
|
|
|
LinkedModule->getFunction("ba_func"));
|
|
|
|
EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
|
|
|
|
LinkedModule->getFunction("ba_func"));
|
|
|
|
|
|
|
|
delete LinkedModule;
|
|
|
|
}
|
|
|
|
|
2014-10-28 08:24:16 +08:00
|
|
|
static Module *getInternal(LLVMContext &Ctx) {
|
2014-01-16 14:29:36 +08:00
|
|
|
Module *InternalM = new Module("InternalModule", Ctx);
|
2014-04-29 23:52:46 +08:00
|
|
|
FunctionType *FTy = FunctionType::get(
|
|
|
|
Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
|
2014-01-16 14:29:36 +08:00
|
|
|
|
2014-10-28 08:24:16 +08:00
|
|
|
Function *F =
|
|
|
|
Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
|
2014-01-16 14:29:36 +08:00
|
|
|
F->setCallingConv(CallingConv::C);
|
|
|
|
|
|
|
|
BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
|
|
|
|
StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
|
|
|
|
|
|
|
|
GlobalVariable *GV =
|
2014-04-29 23:52:46 +08:00
|
|
|
new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
|
2014-06-09 06:29:17 +08:00
|
|
|
GlobalValue::InternalLinkage, nullptr, "g");
|
2014-01-16 14:29:36 +08:00
|
|
|
|
|
|
|
GV->setInitializer(ConstantStruct::get(STy, F));
|
2014-10-28 08:24:16 +08:00
|
|
|
return InternalM;
|
|
|
|
}
|
2014-01-16 14:29:36 +08:00
|
|
|
|
2014-10-28 08:24:16 +08:00
|
|
|
TEST_F(LinkModuleTest, EmptyModule) {
|
|
|
|
std::unique_ptr<Module> InternalM(getInternal(Ctx));
|
|
|
|
std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
|
|
|
|
Linker::LinkModules(EmptyM.get(), InternalM.get());
|
|
|
|
}
|
2014-01-16 14:29:36 +08:00
|
|
|
|
2014-10-28 08:24:16 +08:00
|
|
|
TEST_F(LinkModuleTest, EmptyModule2) {
|
|
|
|
std::unique_ptr<Module> InternalM(getInternal(Ctx));
|
|
|
|
std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
|
|
|
|
Linker::LinkModules(InternalM.get(), EmptyM.get());
|
2014-01-16 14:29:36 +08:00
|
|
|
}
|
|
|
|
|
2014-11-18 04:51:01 +08:00
|
|
|
TEST_F(LinkModuleTest, TypeMerge) {
|
|
|
|
LLVMContext C;
|
|
|
|
SMDiagnostic Err;
|
|
|
|
|
|
|
|
const char *M1Str = "%t = type {i32}\n"
|
|
|
|
"@t1 = weak global %t zeroinitializer\n";
|
|
|
|
std::unique_ptr<Module> M1 = parseAssemblyString(M1Str, Err, C);
|
|
|
|
|
|
|
|
const char *M2Str = "%t = type {i32}\n"
|
|
|
|
"@t2 = weak global %t zeroinitializer\n";
|
|
|
|
std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C);
|
|
|
|
|
|
|
|
Linker::LinkModules(M1.get(), M2.get(), [](const llvm::DiagnosticInfo &){});
|
|
|
|
|
|
|
|
EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
|
|
|
|
M1->getNamedGlobal("t2")->getType());
|
|
|
|
}
|
|
|
|
|
2014-01-16 14:29:36 +08:00
|
|
|
} // end anonymous namespace
|