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 | |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 5cb9c82 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 11 | #include "llvm/AsmParser/Parser.h" |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 12 | #include "llvm/IR/BasicBlock.h" |
| 13 | #include "llvm/IR/DataLayout.h" |
| 14 | #include "llvm/IR/Function.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 15 | #include "llvm/IR/IRBuilder.h" |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Module.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 17 | #include "llvm/Linker/Linker.h" |
Rafael Espindola | 5cb9c82 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 18 | #include "llvm/Support/SourceMgr.h" |
Eli Bendersky | ff715e2 | 2015-06-12 23:26:42 +0000 | [diff] [blame] | 19 | #include "llvm-c/Linker.h" |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class LinkModuleTest : public testing::Test { |
| 27 | protected: |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 28 | void SetUp() override { |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 29 | M.reset(new Module("MyModule", Ctx)); |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 30 | FunctionType *FTy = FunctionType::get( |
| 31 | Type::getInt8PtrTy(Ctx), Type::getInt32Ty(Ctx), false /*=isVarArg*/); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 32 | F = Function::Create(FTy, Function::ExternalLinkage, "ba_func", M.get()); |
| 33 | F->setCallingConv(CallingConv::C); |
| 34 | |
| 35 | EntryBB = BasicBlock::Create(Ctx, "entry", F); |
| 36 | SwitchCase1BB = BasicBlock::Create(Ctx, "switch.case.1", F); |
| 37 | SwitchCase2BB = BasicBlock::Create(Ctx, "switch.case.2", F); |
| 38 | ExitBB = BasicBlock::Create(Ctx, "exit", F); |
| 39 | |
David Blaikie | 156d46e | 2015-03-24 23:34:31 +0000 | [diff] [blame] | 40 | AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 41 | |
| 42 | GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 43 | GlobalValue::InternalLinkage, nullptr,"switch.bas"); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 44 | |
| 45 | // Global Initializer |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 46 | std::vector<Constant *> Init; |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 47 | Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB); |
| 48 | Init.push_back(SwitchCase1BA); |
| 49 | |
| 50 | Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB); |
| 51 | Init.push_back(SwitchCase2BA); |
| 52 | |
| 53 | ConstantInt *One = ConstantInt::get(Type::getInt32Ty(Ctx), 1); |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 54 | Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr, One, |
| 55 | Type::getInt8PtrTy(Ctx)); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 56 | Init.push_back(OnePtr); |
| 57 | |
| 58 | GV->setInitializer(ConstantArray::get(AT, Init)); |
| 59 | } |
| 60 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 61 | void TearDown() override { M.reset(); } |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 62 | |
NAKAMURA Takumi | b49b99b | 2014-04-29 15:52:27 +0000 | [diff] [blame] | 63 | LLVMContext Ctx; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 64 | std::unique_ptr<Module> M; |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 65 | Function *F; |
David Blaikie | 156d46e | 2015-03-24 23:34:31 +0000 | [diff] [blame] | 66 | ArrayType *AT; |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 67 | GlobalVariable *GV; |
| 68 | BasicBlock *EntryBB; |
| 69 | BasicBlock *SwitchCase1BB; |
| 70 | BasicBlock *SwitchCase2BB; |
| 71 | BasicBlock *ExitBB; |
| 72 | }; |
| 73 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 74 | static void expectNoDiags(const DiagnosticInfo &DI, void *C) { |
| 75 | EXPECT_TRUE(false); |
| 76 | } |
Rafael Espindola | f49a38f | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 77 | |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 78 | TEST_F(LinkModuleTest, BlockAddress) { |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 79 | IRBuilder<> Builder(EntryBB); |
| 80 | |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 81 | std::vector<Value *> GEPIndices; |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 82 | GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0)); |
Duncan P. N. Exon Smith | c8925b1 | 2015-10-20 18:30:20 +0000 | [diff] [blame] | 83 | GEPIndices.push_back(&*F->arg_begin()); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 84 | |
David Blaikie | 156d46e | 2015-03-24 23:34:31 +0000 | [diff] [blame] | 85 | Value *GEP = Builder.CreateGEP(AT, GV, GEPIndices, "switch.gep"); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 86 | Value *Load = Builder.CreateLoad(GEP, "switch.load"); |
| 87 | |
| 88 | Builder.CreateRet(Load); |
| 89 | |
| 90 | Builder.SetInsertPoint(SwitchCase1BB); |
| 91 | Builder.CreateBr(ExitBB); |
| 92 | |
| 93 | Builder.SetInsertPoint(SwitchCase2BB); |
| 94 | Builder.CreateBr(ExitBB); |
| 95 | |
| 96 | Builder.SetInsertPoint(ExitBB); |
| 97 | Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx))); |
| 98 | |
NAKAMURA Takumi | b49b99b | 2014-04-29 15:52:27 +0000 | [diff] [blame] | 99 | Module *LinkedModule = new Module("MyModuleLinked", Ctx); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 100 | Ctx.setDiagnosticHandler(expectNoDiags); |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame^] | 101 | Linker::linkModules(*LinkedModule, std::move(M)); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 102 | |
| 103 | // Check that the global "@switch.bas" is well-formed. |
| 104 | const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas"); |
| 105 | const Constant *Init = LinkedGV->getInitializer(); |
| 106 | |
| 107 | // @switch.bas = internal global [3 x i8*] |
| 108 | // [i8* blockaddress(@ba_func, %switch.case.1), |
| 109 | // i8* blockaddress(@ba_func, %switch.case.2), |
| 110 | // i8* inttoptr (i32 1 to i8*)] |
| 111 | |
| 112 | ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3); |
| 113 | EXPECT_EQ(AT, Init->getType()); |
| 114 | |
| 115 | Value *Elem = Init->getOperand(0); |
| 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")); |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 121 | |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 122 | Elem = Init->getOperand(1); |
| 123 | ASSERT_TRUE(isa<BlockAddress>(Elem)); |
| 124 | EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(), |
| 125 | LinkedModule->getFunction("ba_func")); |
| 126 | EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(), |
| 127 | LinkedModule->getFunction("ba_func")); |
| 128 | |
| 129 | delete LinkedModule; |
| 130 | } |
| 131 | |
Eli Bendersky | ff715e2 | 2015-06-12 23:26:42 +0000 | [diff] [blame] | 132 | static Module *getExternal(LLVMContext &Ctx, StringRef FuncName) { |
| 133 | // Create a module with an empty externally-linked function |
| 134 | Module *M = new Module("ExternalModule", Ctx); |
| 135 | FunctionType *FTy = FunctionType::get( |
| 136 | Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/); |
| 137 | |
| 138 | Function *F = |
| 139 | Function::Create(FTy, Function::ExternalLinkage, FuncName, M); |
| 140 | F->setCallingConv(CallingConv::C); |
| 141 | |
| 142 | BasicBlock *BB = BasicBlock::Create(Ctx, "", F); |
| 143 | IRBuilder<> Builder(BB); |
| 144 | Builder.CreateRetVoid(); |
| 145 | return M; |
| 146 | } |
| 147 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 148 | static Module *getInternal(LLVMContext &Ctx) { |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 149 | Module *InternalM = new Module("InternalModule", Ctx); |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 150 | FunctionType *FTy = FunctionType::get( |
| 151 | Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 152 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 153 | Function *F = |
| 154 | Function::Create(FTy, Function::InternalLinkage, "bar", InternalM); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 155 | F->setCallingConv(CallingConv::C); |
| 156 | |
| 157 | BasicBlock *BB = BasicBlock::Create(Ctx, "", F); |
| 158 | IRBuilder<> Builder(BB); |
| 159 | Builder.CreateRetVoid(); |
| 160 | |
| 161 | StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0)); |
| 162 | |
| 163 | GlobalVariable *GV = |
NAKAMURA Takumi | 0f250ed | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 164 | new GlobalVariable(*InternalM, STy, false /*=isConstant*/, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 165 | GlobalValue::InternalLinkage, nullptr, "g"); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 166 | |
| 167 | GV->setInitializer(ConstantStruct::get(STy, F)); |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 168 | return InternalM; |
| 169 | } |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 170 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 171 | TEST_F(LinkModuleTest, EmptyModule) { |
| 172 | std::unique_ptr<Module> InternalM(getInternal(Ctx)); |
| 173 | std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx)); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 174 | Ctx.setDiagnosticHandler(expectNoDiags); |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame^] | 175 | Linker::linkModules(*EmptyM, std::move(InternalM)); |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 176 | } |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 177 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 178 | TEST_F(LinkModuleTest, EmptyModule2) { |
| 179 | std::unique_ptr<Module> InternalM(getInternal(Ctx)); |
| 180 | std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx)); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 181 | Ctx.setDiagnosticHandler(expectNoDiags); |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame^] | 182 | Linker::linkModules(*InternalM, std::move(EmptyM)); |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Rafael Espindola | 5cb9c82 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 185 | TEST_F(LinkModuleTest, TypeMerge) { |
| 186 | LLVMContext C; |
| 187 | SMDiagnostic Err; |
| 188 | |
| 189 | const char *M1Str = "%t = type {i32}\n" |
| 190 | "@t1 = weak global %t zeroinitializer\n"; |
| 191 | std::unique_ptr<Module> M1 = parseAssemblyString(M1Str, Err, C); |
| 192 | |
| 193 | const char *M2Str = "%t = type {i32}\n" |
| 194 | "@t2 = weak global %t zeroinitializer\n"; |
| 195 | std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C); |
| 196 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 197 | Ctx.setDiagnosticHandler(expectNoDiags); |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame^] | 198 | Linker::linkModules(*M1, std::move(M2)); |
Rafael Espindola | 5cb9c82 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 199 | |
| 200 | EXPECT_EQ(M1->getNamedGlobal("t1")->getType(), |
| 201 | M1->getNamedGlobal("t2")->getType()); |
| 202 | } |
| 203 | |
Eli Bendersky | ff715e2 | 2015-06-12 23:26:42 +0000 | [diff] [blame] | 204 | TEST_F(LinkModuleTest, CAPISuccess) { |
| 205 | std::unique_ptr<Module> DestM(getExternal(Ctx, "foo")); |
| 206 | std::unique_ptr<Module> SourceM(getExternal(Ctx, "bar")); |
| 207 | char *errout = nullptr; |
| 208 | LLVMBool result = LLVMLinkModules(wrap(DestM.get()), wrap(SourceM.get()), |
| 209 | LLVMLinkerDestroySource, &errout); |
| 210 | EXPECT_EQ(0, result); |
| 211 | EXPECT_EQ(nullptr, errout); |
| 212 | // "bar" is present in destination module |
| 213 | EXPECT_NE(nullptr, DestM->getFunction("bar")); |
| 214 | } |
| 215 | |
| 216 | TEST_F(LinkModuleTest, CAPIFailure) { |
| 217 | // Symbol clash between two modules |
| 218 | std::unique_ptr<Module> DestM(getExternal(Ctx, "foo")); |
| 219 | std::unique_ptr<Module> SourceM(getExternal(Ctx, "foo")); |
| 220 | char *errout = nullptr; |
| 221 | LLVMBool result = LLVMLinkModules(wrap(DestM.get()), wrap(SourceM.get()), |
| 222 | LLVMLinkerDestroySource, &errout); |
| 223 | EXPECT_EQ(1, result); |
| 224 | EXPECT_STREQ("Linking globals named 'foo': symbol multiply defined!", errout); |
Benjamin Kramer | f1d570d | 2015-06-15 15:42:26 +0000 | [diff] [blame] | 225 | LLVMDisposeMessage(errout); |
Eli Bendersky | ff715e2 | 2015-06-12 23:26:42 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame^] | 228 | TEST_F(LinkModuleTest, NewCAPISuccess) { |
| 229 | std::unique_ptr<Module> DestM(getExternal(Ctx, "foo")); |
| 230 | std::unique_ptr<Module> SourceM(getExternal(Ctx, "bar")); |
| 231 | LLVMBool Result = |
| 232 | LLVMLinkModules2(wrap(DestM.get()), wrap(SourceM.release())); |
| 233 | EXPECT_EQ(0, Result); |
| 234 | // "bar" is present in destination module |
| 235 | EXPECT_NE(nullptr, DestM->getFunction("bar")); |
| 236 | } |
| 237 | |
| 238 | static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) { |
| 239 | auto *Err = reinterpret_cast<std::string *>(C); |
| 240 | char *CErr = LLVMGetDiagInfoDescription(DI); |
| 241 | *Err = CErr; |
| 242 | LLVMDisposeMessage(CErr); |
| 243 | } |
| 244 | |
| 245 | TEST_F(LinkModuleTest, NewCAPIFailure) { |
| 246 | // Symbol clash between two modules |
| 247 | LLVMContext Ctx; |
| 248 | std::string Err; |
| 249 | LLVMContextSetDiagnosticHandler(wrap(&Ctx), diagnosticHandler, &Err); |
| 250 | |
| 251 | std::unique_ptr<Module> DestM(getExternal(Ctx, "foo")); |
| 252 | std::unique_ptr<Module> SourceM(getExternal(Ctx, "foo")); |
| 253 | LLVMBool Result = |
| 254 | LLVMLinkModules2(wrap(DestM.get()), wrap(SourceM.release())); |
| 255 | EXPECT_EQ(1, Result); |
| 256 | EXPECT_EQ("Linking globals named 'foo': symbol multiply defined!", Err); |
| 257 | } |
| 258 | |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 259 | TEST_F(LinkModuleTest, MoveDistinctMDs) { |
| 260 | LLVMContext C; |
| 261 | SMDiagnostic Err; |
| 262 | |
| 263 | const char *SrcStr = "define void @foo() !attach !0 {\n" |
| 264 | "entry:\n" |
| 265 | " call void @llvm.md(metadata !1)\n" |
| 266 | " ret void, !attach !2\n" |
| 267 | "}\n" |
| 268 | "declare void @llvm.md(metadata)\n" |
| 269 | "!named = !{!3, !4}\n" |
| 270 | "!0 = distinct !{}\n" |
| 271 | "!1 = distinct !{}\n" |
| 272 | "!2 = distinct !{}\n" |
| 273 | "!3 = distinct !{}\n" |
| 274 | "!4 = !{!3}\n"; |
| 275 | |
| 276 | std::unique_ptr<Module> Src = parseAssemblyString(SrcStr, Err, C); |
| 277 | assert(Src); |
| 278 | ASSERT_TRUE(Src.get()); |
| 279 | |
| 280 | // Get the addresses of the Metadata before merging. |
| 281 | Function *F = &*Src->begin(); |
| 282 | ASSERT_EQ("foo", F->getName()); |
| 283 | BasicBlock *BB = &F->getEntryBlock(); |
| 284 | auto *CI = cast<CallInst>(&BB->front()); |
| 285 | auto *RI = cast<ReturnInst>(BB->getTerminator()); |
| 286 | NamedMDNode *NMD = &*Src->named_metadata_begin(); |
| 287 | |
| 288 | MDNode *M0 = F->getMetadata("attach"); |
| 289 | MDNode *M1 = |
| 290 | cast<MDNode>(cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata()); |
| 291 | MDNode *M2 = RI->getMetadata("attach"); |
| 292 | MDNode *M3 = NMD->getOperand(0); |
| 293 | MDNode *M4 = NMD->getOperand(1); |
| 294 | |
| 295 | // Confirm a few things about the IR. |
| 296 | EXPECT_TRUE(M0->isDistinct()); |
| 297 | EXPECT_TRUE(M1->isDistinct()); |
| 298 | EXPECT_TRUE(M2->isDistinct()); |
| 299 | EXPECT_TRUE(M3->isDistinct()); |
| 300 | EXPECT_TRUE(M4->isUniqued()); |
| 301 | EXPECT_EQ(M3, M4->getOperand(0)); |
| 302 | |
| 303 | // Link into destination module. |
| 304 | auto Dst = llvm::make_unique<Module>("Linked", C); |
| 305 | ASSERT_TRUE(Dst.get()); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 306 | Ctx.setDiagnosticHandler(expectNoDiags); |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame^] | 307 | Linker::linkModules(*Dst, std::move(Src)); |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 308 | |
| 309 | // Check that distinct metadata was moved, not cloned. Even !4, the uniqued |
| 310 | // node, should effectively be moved, since its only operand hasn't changed. |
| 311 | F = &*Dst->begin(); |
| 312 | BB = &F->getEntryBlock(); |
| 313 | CI = cast<CallInst>(&BB->front()); |
| 314 | RI = cast<ReturnInst>(BB->getTerminator()); |
| 315 | NMD = &*Dst->named_metadata_begin(); |
| 316 | |
| 317 | EXPECT_EQ(M0, F->getMetadata("attach")); |
| 318 | EXPECT_EQ(M1, cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata()); |
| 319 | EXPECT_EQ(M2, RI->getMetadata("attach")); |
| 320 | EXPECT_EQ(M3, NMD->getOperand(0)); |
| 321 | EXPECT_EQ(M4, NMD->getOperand(1)); |
| 322 | |
| 323 | // Confirm a few things about the IR. This shouldn't have changed. |
| 324 | EXPECT_TRUE(M0->isDistinct()); |
| 325 | EXPECT_TRUE(M1->isDistinct()); |
| 326 | EXPECT_TRUE(M2->isDistinct()); |
| 327 | EXPECT_TRUE(M3->isDistinct()); |
| 328 | EXPECT_TRUE(M4->isUniqued()); |
| 329 | EXPECT_EQ(M3, M4->getOperand(0)); |
| 330 | } |
| 331 | |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 332 | } // end anonymous namespace |