From ea93373a0a1bf1d087f5414e566384c2af3ebf09 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 20 Dec 2011 00:12:26 +0000 Subject: [PATCH] Now that PR11464 is fixed, reapply the patch to fix PR11464, merging types by name when we can. We still don't guarantee type name linkage but we do it when obviously the right thing to do. This makes LTO type names easier to read, for example. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@146932 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Linker/LinkModules.cpp | 25 +++++++++++++++++++++++++ test/Linker/link-type-names.ll | 10 ++++++++++ 2 files changed, 35 insertions(+) create mode 100644 test/Linker/link-type-names.ll diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 7706b9064e5..4f6013e7020 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -558,6 +558,31 @@ void ModuleLinker::computeTypeMapping() { TypeMap.addTypeMapping(DGV->getType(), I->getType()); } + // Incorporate types by name, scanning all the types in the source module. + // At this point, the destination module may have a type "%foo = { i32 }" for + // example. When the source module got loaded into the same LLVMContext, if + // it had the same type, it would have been renamed to "%foo.42 = { i32 }". + // Though it isn't required for correctness, attempt to link these up to clean + // up the IR. + std::vector SrcStructTypes; + SrcM->findUsedStructTypes(SrcStructTypes); + + for (unsigned i = 0, e = SrcStructTypes.size(); i != e; ++i) { + StructType *ST = SrcStructTypes[i]; + if (!ST->hasName()) continue; + + // Check to see if there is a dot in the name followed by a digit. + size_t DotPos = ST->getName().rfind('.'); + if (DotPos == 0 || DotPos == StringRef::npos || + ST->getName().back() == '.' || !isdigit(ST->getName()[DotPos+1])) + continue; + + // Check to see if the destination module has a struct with the prefix name. + if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos))) + TypeMap.addTypeMapping(DST, ST); + } + + // Don't bother incorporating aliases, they aren't generally typed well. // Now that we have discovered all of the type equivalences, get a body for diff --git a/test/Linker/link-type-names.ll b/test/Linker/link-type-names.ll new file mode 100644 index 00000000000..bfc3b64361d --- /dev/null +++ b/test/Linker/link-type-names.ll @@ -0,0 +1,10 @@ +; RUN: echo "%X = type { i32 } @G2 = global %X { i32 4 }" > %t.ll +; RUN: llvm-link %s %t.ll -S | FileCheck %s +; PR11464 + +%X = type { i32 } +@G = global %X { i32 4 } + + +; CHECK: @G = global %X { i32 4 } +; CHECK: @G2 = global %X { i32 4 }