Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | 5cb9c82 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 10 | #include "llvm/AsmParser/Parser.h" |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 11 | #include "llvm/IR/BasicBlock.h" |
| 12 | #include "llvm/IR/DataLayout.h" |
| 13 | #include "llvm/IR/Function.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 14 | #include "llvm/IR/IRBuilder.h" |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Module.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame^] | 16 | #include "llvm/Linker/Linker.h" |
Rafael Espindola | 5cb9c82 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 17 | #include "llvm/Support/SourceMgr.h" |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 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() { |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 27 | M.reset(new Module("MyModule", Ctx)); |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 28 | FunctionType *FTy = FunctionType::get( |
| 29 | Type::getInt8PtrTy(Ctx), Type::getInt32Ty(Ctx), false /*=isVarArg*/); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 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*/, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 41 | GlobalValue::InternalLinkage, nullptr,"switch.bas"); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 42 | |
| 43 | // Global Initializer |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 44 | std::vector<Constant *> Init; |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 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); |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 52 | Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr, One, |
| 53 | Type::getInt8PtrTy(Ctx)); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 54 | Init.push_back(OnePtr); |
| 55 | |
| 56 | GV->setInitializer(ConstantArray::get(AT, Init)); |
| 57 | } |
| 58 | |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 59 | virtual void TearDown() { M.reset(); } |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 60 | |
NAKAMURA Takumi | b49b99b | 2014-04-29 15:52:27 +0000 | [diff] [blame] | 61 | LLVMContext Ctx; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 62 | std::unique_ptr<Module> M; |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 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) { |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 72 | IRBuilder<> Builder(EntryBB); |
| 73 | |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 74 | std::vector<Value *> GEPIndices; |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 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 | |
NAKAMURA Takumi | b49b99b | 2014-04-29 15:52:27 +0000 | [diff] [blame] | 92 | Module *LinkedModule = new Module("MyModuleLinked", Ctx); |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 93 | Linker::LinkModules(LinkedModule, M.get()); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 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")); |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 116 | |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 127 | static Module *getInternal(LLVMContext &Ctx) { |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 128 | Module *InternalM = new Module("InternalModule", Ctx); |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 129 | FunctionType *FTy = FunctionType::get( |
| 130 | Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 131 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 132 | Function *F = |
| 133 | Function::Create(FTy, Function::InternalLinkage, "bar", InternalM); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 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 = |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 143 | new GlobalVariable(*InternalM, STy, false /*=isConstant*/, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 144 | GlobalValue::InternalLinkage, nullptr, "g"); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 145 | |
| 146 | GV->setInitializer(ConstantStruct::get(STy, F)); |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 147 | return InternalM; |
| 148 | } |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 149 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 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 | } |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 155 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 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()); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Rafael Espindola | 5cb9c82 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 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 | |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 180 | } // end anonymous namespace |