Mikhail Glushenkov | c834bbf | 2009-03-03 10:04:23 +0000 | [diff] [blame] | 1 | //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the LLVM module linker. |
| 11 | // |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 7cc371a | 2004-11-14 23:27:04 +0000 | [diff] [blame] | 14 | #include "llvm/Linker.h" |
Chris Lattner | adbc0b5 | 2003-11-20 18:23:14 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/DerivedTypes.h" |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseSet.h" |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Optional.h" |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SetVector.h" |
| 22 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Cloning.h" |
Dan Gohman | 05ea54e | 2010-08-24 18:50:07 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Duncan Sands | 0aaf2f6 | 2012-03-03 09:36:58 +0000 | [diff] [blame] | 27 | #include <cctype> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // TypeMap implementation. |
| 32 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 34 | namespace { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 35 | class TypeMapTy : public ValueMapTypeRemapper { |
| 36 | /// MappedTypes - This is a mapping from a source type to a destination type |
| 37 | /// to use. |
| 38 | DenseMap<Type*, Type*> MappedTypes; |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 40 | /// SpeculativeTypes - When checking to see if two subgraphs are isomorphic, |
| 41 | /// we speculatively add types to MappedTypes, but keep track of them here in |
| 42 | /// case we need to roll back. |
| 43 | SmallVector<Type*, 16> SpeculativeTypes; |
| 44 | |
Chris Lattner | 6891050 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 45 | /// SrcDefinitionsToResolve - This is a list of non-opaque structs in the |
| 46 | /// source module that are mapped to an opaque struct in the destination |
| 47 | /// module. |
| 48 | SmallVector<StructType*, 16> SrcDefinitionsToResolve; |
| 49 | |
| 50 | /// DstResolvedOpaqueTypes - This is the set of opaque types in the |
| 51 | /// destination modules who are getting a body from the source module. |
| 52 | SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes; |
Chris Lattner | fc196f9 | 2008-06-16 23:06:51 +0000 | [diff] [blame] | 53 | public: |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 55 | /// addTypeMapping - Indicate that the specified type in the destination |
| 56 | /// module is conceptually equivalent to the specified type in the source |
| 57 | /// module. |
| 58 | void addTypeMapping(Type *DstTy, Type *SrcTy); |
| 59 | |
| 60 | /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest |
| 61 | /// module from a type definition in the source module. |
| 62 | void linkDefinedTypeBodies(); |
| 63 | |
| 64 | /// get - Return the mapped type to use for the specified input type from the |
| 65 | /// source module. |
| 66 | Type *get(Type *SrcTy); |
| 67 | |
| 68 | FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));} |
| 69 | |
| 70 | private: |
| 71 | Type *getImpl(Type *T); |
| 72 | /// remapType - Implement the ValueMapTypeRemapper interface. |
| 73 | Type *remapType(Type *SrcTy) { |
| 74 | return get(SrcTy); |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 75 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 76 | |
| 77 | bool areTypesIsomorphic(Type *DstTy, Type *SrcTy); |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 78 | }; |
| 79 | } |
| 80 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 81 | void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) { |
| 82 | Type *&Entry = MappedTypes[SrcTy]; |
| 83 | if (Entry) return; |
| 84 | |
| 85 | if (DstTy == SrcTy) { |
| 86 | Entry = DstTy; |
| 87 | return; |
| 88 | } |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 90 | // Check to see if these types are recursively isomorphic and establish a |
| 91 | // mapping between them if so. |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 92 | if (!areTypesIsomorphic(DstTy, SrcTy)) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 93 | // Oops, they aren't isomorphic. Just discard this request by rolling out |
| 94 | // any speculative mappings we've established. |
| 95 | for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i) |
| 96 | MappedTypes.erase(SpeculativeTypes[i]); |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 98 | SpeculativeTypes.clear(); |
| 99 | } |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 101 | /// areTypesIsomorphic - Recursively walk this pair of types, returning true |
| 102 | /// if they are isomorphic, false if they are not. |
| 103 | bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) { |
| 104 | // Two types with differing kinds are clearly not isomorphic. |
| 105 | if (DstTy->getTypeID() != SrcTy->getTypeID()) return false; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 107 | // If we have an entry in the MappedTypes table, then we have our answer. |
| 108 | Type *&Entry = MappedTypes[SrcTy]; |
| 109 | if (Entry) |
| 110 | return Entry == DstTy; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 112 | // Two identical types are clearly isomorphic. Remember this |
| 113 | // non-speculatively. |
| 114 | if (DstTy == SrcTy) { |
| 115 | Entry = DstTy; |
Chris Lattner | 5653965 | 2008-06-16 20:03:01 +0000 | [diff] [blame] | 116 | return true; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 117 | } |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 119 | // Okay, we have two types with identical kinds that we haven't seen before. |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 121 | // If this is an opaque struct type, special case it. |
| 122 | if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) { |
| 123 | // Mapping an opaque type to any struct, just keep the dest struct. |
| 124 | if (SSTy->isOpaque()) { |
| 125 | Entry = DstTy; |
| 126 | SpeculativeTypes.push_back(SrcTy); |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 127 | return true; |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 128 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 6891050 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 130 | // Mapping a non-opaque source type to an opaque dest. If this is the first |
| 131 | // type that we're mapping onto this destination type then we succeed. Keep |
| 132 | // the dest, but fill it in later. This doesn't need to be speculative. If |
| 133 | // this is the second (different) type that we're trying to map onto the |
| 134 | // same opaque type then we fail. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 135 | if (cast<StructType>(DstTy)->isOpaque()) { |
Chris Lattner | 6891050 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 136 | // We can only map one source type onto the opaque destination type. |
| 137 | if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy))) |
| 138 | return false; |
| 139 | SrcDefinitionsToResolve.push_back(SSTy); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 140 | Entry = DstTy; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // If the number of subtypes disagree between the two types, then we fail. |
| 146 | if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes()) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 147 | return false; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 148 | |
| 149 | // Fail if any of the extra properties (e.g. array size) of the type disagree. |
| 150 | if (isa<IntegerType>(DstTy)) |
| 151 | return false; // bitwidth disagrees. |
| 152 | if (PointerType *PT = dyn_cast<PointerType>(DstTy)) { |
| 153 | if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace()) |
| 154 | return false; |
Chris Lattner | 1a31f3b | 2011-12-20 23:14:57 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 156 | } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) { |
| 157 | if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg()) |
| 158 | return false; |
| 159 | } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) { |
| 160 | StructType *SSTy = cast<StructType>(SrcTy); |
Chris Lattner | 1bcbf85 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 161 | if (DSTy->isLiteral() != SSTy->isLiteral() || |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 162 | DSTy->isPacked() != SSTy->isPacked()) |
| 163 | return false; |
| 164 | } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) { |
| 165 | if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements()) |
| 166 | return false; |
| 167 | } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
| 168 | if (DVTy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements()) |
| 169 | return false; |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 171 | |
| 172 | // Otherwise, we speculate that these two types will line up and recursively |
| 173 | // check the subelements. |
| 174 | Entry = DstTy; |
| 175 | SpeculativeTypes.push_back(SrcTy); |
| 176 | |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 177 | for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i) |
| 178 | if (!areTypesIsomorphic(DstTy->getContainedType(i), |
| 179 | SrcTy->getContainedType(i))) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 180 | return false; |
| 181 | |
| 182 | // If everything seems to have lined up, then everything is great. |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest |
| 187 | /// module from a type definition in the source module. |
| 188 | void TypeMapTy::linkDefinedTypeBodies() { |
| 189 | SmallVector<Type*, 16> Elements; |
| 190 | SmallString<16> TmpName; |
| 191 | |
| 192 | // Note that processing entries in this loop (calling 'get') can add new |
Chris Lattner | 6891050 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 193 | // entries to the SrcDefinitionsToResolve vector. |
| 194 | while (!SrcDefinitionsToResolve.empty()) { |
| 195 | StructType *SrcSTy = SrcDefinitionsToResolve.pop_back_val(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 196 | StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]); |
| 197 | |
| 198 | // TypeMap is a many-to-one mapping, if there were multiple types that |
| 199 | // provide a body for DstSTy then previous iterations of this loop may have |
| 200 | // already handled it. Just ignore this case. |
| 201 | if (!DstSTy->isOpaque()) continue; |
| 202 | assert(!SrcSTy->isOpaque() && "Not resolving a definition?"); |
| 203 | |
| 204 | // Map the body of the source type over to a new body for the dest type. |
| 205 | Elements.resize(SrcSTy->getNumElements()); |
| 206 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) |
| 207 | Elements[i] = getImpl(SrcSTy->getElementType(i)); |
| 208 | |
| 209 | DstSTy->setBody(Elements, SrcSTy->isPacked()); |
| 210 | |
| 211 | // If DstSTy has no name or has a longer name than STy, then viciously steal |
| 212 | // STy's name. |
| 213 | if (!SrcSTy->hasName()) continue; |
| 214 | StringRef SrcName = SrcSTy->getName(); |
| 215 | |
| 216 | if (!DstSTy->hasName() || DstSTy->getName().size() > SrcName.size()) { |
| 217 | TmpName.insert(TmpName.end(), SrcName.begin(), SrcName.end()); |
| 218 | SrcSTy->setName(""); |
| 219 | DstSTy->setName(TmpName.str()); |
| 220 | TmpName.clear(); |
| 221 | } |
| 222 | } |
Chris Lattner | 6891050 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 223 | |
| 224 | DstResolvedOpaqueTypes.clear(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 228 | /// get - Return the mapped type to use for the specified input type from the |
| 229 | /// source module. |
| 230 | Type *TypeMapTy::get(Type *Ty) { |
| 231 | Type *Result = getImpl(Ty); |
| 232 | |
| 233 | // If this caused a reference to any struct type, resolve it before returning. |
Chris Lattner | 6891050 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 234 | if (!SrcDefinitionsToResolve.empty()) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 235 | linkDefinedTypeBodies(); |
| 236 | return Result; |
| 237 | } |
| 238 | |
| 239 | /// getImpl - This is the recursive version of get(). |
| 240 | Type *TypeMapTy::getImpl(Type *Ty) { |
| 241 | // If we already have an entry for this type, return it. |
| 242 | Type **Entry = &MappedTypes[Ty]; |
| 243 | if (*Entry) return *Entry; |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 245 | // If this is not a named struct type, then just map all of the elements and |
| 246 | // then rebuild the type from inside out. |
Chris Lattner | 1bcbf85 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 247 | if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 248 | // If there are no element types to map, then the type is itself. This is |
| 249 | // true for the anonymous {} struct, things like 'float', integers, etc. |
| 250 | if (Ty->getNumContainedTypes() == 0) |
| 251 | return *Entry = Ty; |
| 252 | |
| 253 | // Remap all of the elements, keeping track of whether any of them change. |
| 254 | bool AnyChange = false; |
| 255 | SmallVector<Type*, 4> ElementTypes; |
| 256 | ElementTypes.resize(Ty->getNumContainedTypes()); |
| 257 | for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i) { |
| 258 | ElementTypes[i] = getImpl(Ty->getContainedType(i)); |
| 259 | AnyChange |= ElementTypes[i] != Ty->getContainedType(i); |
| 260 | } |
| 261 | |
| 262 | // If we found our type while recursively processing stuff, just use it. |
| 263 | Entry = &MappedTypes[Ty]; |
| 264 | if (*Entry) return *Entry; |
| 265 | |
| 266 | // If all of the element types mapped directly over, then the type is usable |
| 267 | // as-is. |
| 268 | if (!AnyChange) |
| 269 | return *Entry = Ty; |
| 270 | |
| 271 | // Otherwise, rebuild a modified type. |
| 272 | switch (Ty->getTypeID()) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 273 | default: llvm_unreachable("unknown derived type to remap"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 274 | case Type::ArrayTyID: |
| 275 | return *Entry = ArrayType::get(ElementTypes[0], |
| 276 | cast<ArrayType>(Ty)->getNumElements()); |
| 277 | case Type::VectorTyID: |
| 278 | return *Entry = VectorType::get(ElementTypes[0], |
| 279 | cast<VectorType>(Ty)->getNumElements()); |
| 280 | case Type::PointerTyID: |
| 281 | return *Entry = PointerType::get(ElementTypes[0], |
| 282 | cast<PointerType>(Ty)->getAddressSpace()); |
| 283 | case Type::FunctionTyID: |
| 284 | return *Entry = FunctionType::get(ElementTypes[0], |
Frits van Bommel | 39b5abf | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 285 | makeArrayRef(ElementTypes).slice(1), |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 286 | cast<FunctionType>(Ty)->isVarArg()); |
| 287 | case Type::StructTyID: |
| 288 | // Note that this is only reached for anonymous structs. |
| 289 | return *Entry = StructType::get(Ty->getContext(), ElementTypes, |
| 290 | cast<StructType>(Ty)->isPacked()); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Otherwise, this is an unmapped named struct. If the struct can be directly |
| 295 | // mapped over, just use it as-is. This happens in a case when the linked-in |
| 296 | // module has something like: |
| 297 | // %T = type {%T*, i32} |
| 298 | // @GV = global %T* null |
| 299 | // where T does not exist at all in the destination module. |
| 300 | // |
| 301 | // The other case we watch for is when the type is not in the destination |
| 302 | // module, but that it has to be rebuilt because it refers to something that |
| 303 | // is already mapped. For example, if the destination module has: |
| 304 | // %A = type { i32 } |
| 305 | // and the source module has something like |
| 306 | // %A' = type { i32 } |
| 307 | // %B = type { %A'* } |
| 308 | // @GV = global %B* null |
| 309 | // then we want to create a new type: "%B = type { %A*}" and have it take the |
| 310 | // pristine "%B" name from the source module. |
| 311 | // |
| 312 | // To determine which case this is, we have to recursively walk the type graph |
| 313 | // speculating that we'll be able to reuse it unmodified. Only if this is |
| 314 | // safe would we map the entire thing over. Because this is an optimization, |
| 315 | // and is not required for the prettiness of the linked module, we just skip |
| 316 | // it and always rebuild a type here. |
| 317 | StructType *STy = cast<StructType>(Ty); |
| 318 | |
| 319 | // If the type is opaque, we can just use it directly. |
| 320 | if (STy->isOpaque()) |
| 321 | return *Entry = STy; |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 323 | // Otherwise we create a new type and resolve its body later. This will be |
| 324 | // resolved by the top level of get(). |
Chris Lattner | 6891050 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 325 | SrcDefinitionsToResolve.push_back(STy); |
| 326 | StructType *DTy = StructType::create(STy->getContext()); |
| 327 | DstResolvedOpaqueTypes.insert(DTy); |
| 328 | return *Entry = DTy; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 331 | |
| 332 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 333 | //===----------------------------------------------------------------------===// |
| 334 | // ModuleLinker implementation. |
| 335 | //===----------------------------------------------------------------------===// |
| 336 | |
| 337 | namespace { |
| 338 | /// ModuleLinker - This is an implementation class for the LinkModules |
| 339 | /// function, which is the entrypoint for this file. |
| 340 | class ModuleLinker { |
| 341 | Module *DstM, *SrcM; |
| 342 | |
| 343 | TypeMapTy TypeMap; |
| 344 | |
| 345 | /// ValueMap - Mapping of values from what they used to be in Src, to what |
| 346 | /// they are now in DstM. ValueToValueMapTy is a ValueMap, which involves |
| 347 | /// some overhead due to the use of Value handles which the Linker doesn't |
| 348 | /// actually need, but this allows us to reuse the ValueMapper code. |
| 349 | ValueToValueMapTy ValueMap; |
| 350 | |
| 351 | struct AppendingVarInfo { |
| 352 | GlobalVariable *NewGV; // New aggregate global in dest module. |
| 353 | Constant *DstInit; // Old initializer from dest module. |
| 354 | Constant *SrcInit; // Old initializer from src module. |
| 355 | }; |
| 356 | |
| 357 | std::vector<AppendingVarInfo> AppendingVars; |
| 358 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 359 | unsigned Mode; // Mode to treat source module. |
| 360 | |
| 361 | // Set of items not to link in from source. |
| 362 | SmallPtrSet<const Value*, 16> DoNotLinkFromSource; |
| 363 | |
Tanya Lattner | 9af37a3 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 364 | // Vector of functions to lazily link in. |
| 365 | std::vector<Function*> LazilyLinkFunctions; |
| 366 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 367 | public: |
| 368 | std::string ErrorMsg; |
| 369 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 370 | ModuleLinker(Module *dstM, Module *srcM, unsigned mode) |
| 371 | : DstM(dstM), SrcM(srcM), Mode(mode) { } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 372 | |
| 373 | bool run(); |
| 374 | |
| 375 | private: |
| 376 | /// emitError - Helper method for setting a message and returning an error |
| 377 | /// code. |
| 378 | bool emitError(const Twine &Message) { |
| 379 | ErrorMsg = Message.str(); |
Chris Lattner | f6f4f7a | 2008-06-16 18:27:53 +0000 | [diff] [blame] | 380 | return true; |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 381 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 382 | |
| 383 | /// getLinkageResult - This analyzes the two global values and determines |
| 384 | /// what the result will look like in the destination module. |
| 385 | bool getLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 386 | GlobalValue::LinkageTypes <, |
| 387 | GlobalValue::VisibilityTypes &Vis, |
| 388 | bool &LinkFromSrc); |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 389 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 390 | /// getLinkedToGlobal - Given a global in the source module, return the |
| 391 | /// global in the destination module that is being linked to, if any. |
| 392 | GlobalValue *getLinkedToGlobal(GlobalValue *SrcGV) { |
| 393 | // If the source has no name it can't link. If it has local linkage, |
| 394 | // there is no name match-up going on. |
| 395 | if (!SrcGV->hasName() || SrcGV->hasLocalLinkage()) |
| 396 | return 0; |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 398 | // Otherwise see if we have a match in the destination module's symtab. |
| 399 | GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName()); |
| 400 | if (DGV == 0) return 0; |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 401 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 402 | // If we found a global with the same name in the dest module, but it has |
| 403 | // internal linkage, we are really not doing any linkage here. |
| 404 | if (DGV->hasLocalLinkage()) |
| 405 | return 0; |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 407 | // Otherwise, we do in fact link to the destination global. |
| 408 | return DGV; |
| 409 | } |
| 410 | |
| 411 | void computeTypeMapping(); |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 412 | bool categorizeModuleFlagNodes(const NamedMDNode *ModFlags, |
| 413 | DenseMap<MDString*, MDNode*> &ErrorNode, |
| 414 | DenseMap<MDString*, MDNode*> &WarningNode, |
| 415 | DenseMap<MDString*, MDNode*> &OverrideNode, |
| 416 | DenseMap<MDString*, |
| 417 | SmallSetVector<MDNode*, 8> > &RequireNodes, |
| 418 | SmallSetVector<MDString*, 16> &SeenIDs); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 419 | |
| 420 | bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV); |
| 421 | bool linkGlobalProto(GlobalVariable *SrcGV); |
| 422 | bool linkFunctionProto(Function *SrcF); |
| 423 | bool linkAliasProto(GlobalAlias *SrcA); |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 424 | bool linkModuleFlagsMetadata(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 425 | |
| 426 | void linkAppendingVarInit(const AppendingVarInfo &AVI); |
| 427 | void linkGlobalInits(); |
| 428 | void linkFunctionBody(Function *Dst, Function *Src); |
| 429 | void linkAliasBodies(); |
| 430 | void linkNamedMDNodes(); |
| 431 | }; |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 435 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 436 | /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 437 | /// in the symbol table. This is good for all clients except for us. Go |
| 438 | /// through the trouble to force this back. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 439 | static void forceRenaming(GlobalValue *GV, StringRef Name) { |
| 440 | // If the global doesn't force its name or if it already has the right name, |
| 441 | // there is nothing for us to do. |
| 442 | if (GV->hasLocalLinkage() || GV->getName() == Name) |
| 443 | return; |
| 444 | |
| 445 | Module *M = GV->getParent(); |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 446 | |
| 447 | // If there is a conflict, rename the conflict. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 448 | if (GlobalValue *ConflictGV = M->getNamedValue(Name)) { |
Chris Lattner | 33f2949 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 449 | GV->takeName(ConflictGV); |
| 450 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 451 | assert(ConflictGV->getName() != Name && "forceRenaming didn't work"); |
Chris Lattner | 33f2949 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 452 | } else { |
| 453 | GV->setName(Name); // Force the name back |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 454 | } |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 455 | } |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 456 | |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 457 | /// CopyGVAttributes - copy additional attributes (those not needed to construct |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 458 | /// a GlobalValue) from the SrcGV to the DestGV. |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 459 | static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) { |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 460 | // Use the maximum alignment, rather than just copying the alignment of SrcGV. |
| 461 | unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment()); |
| 462 | DestGV->copyAttributesFrom(SrcGV); |
| 463 | DestGV->setAlignment(Alignment); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 464 | |
| 465 | forceRenaming(DestGV, SrcGV->getName()); |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 468 | static bool isLessConstraining(GlobalValue::VisibilityTypes a, |
| 469 | GlobalValue::VisibilityTypes b) { |
| 470 | if (a == GlobalValue::HiddenVisibility) |
| 471 | return false; |
| 472 | if (b == GlobalValue::HiddenVisibility) |
| 473 | return true; |
| 474 | if (a == GlobalValue::ProtectedVisibility) |
| 475 | return false; |
| 476 | if (b == GlobalValue::ProtectedVisibility) |
| 477 | return true; |
| 478 | return false; |
| 479 | } |
| 480 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 481 | /// getLinkageResult - This analyzes the two global values and determines what |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 482 | /// the result will look like in the destination module. In particular, it |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 483 | /// computes the resultant linkage type and visibility, computes whether the |
| 484 | /// global in the source should be copied over to the destination (replacing |
| 485 | /// the existing one), and computes whether this linkage is an error or not. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 486 | bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 487 | GlobalValue::LinkageTypes <, |
| 488 | GlobalValue::VisibilityTypes &Vis, |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 489 | bool &LinkFromSrc) { |
| 490 | assert(Dest && "Must have two globals being queried"); |
| 491 | assert(!Src->hasLocalLinkage() && |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 492 | "If Src has internal linkage, Dest shouldn't be set!"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 493 | |
Peter Collingbourne | 8895316 | 2011-10-30 17:46:34 +0000 | [diff] [blame] | 494 | bool SrcIsDeclaration = Src->isDeclaration() && !Src->isMaterializable(); |
Chris Lattner | f84c59d | 2011-07-14 20:23:05 +0000 | [diff] [blame] | 495 | bool DestIsDeclaration = Dest->isDeclaration(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 496 | |
| 497 | if (SrcIsDeclaration) { |
Anton Korobeynikov | 2b48ef0 | 2008-03-10 22:33:22 +0000 | [diff] [blame] | 498 | // If Src is external or if both Src & Dest are external.. Just link the |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 499 | // external globals, we aren't adding anything. |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 500 | if (Src->hasDLLImportLinkage()) { |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 501 | // If one of GVs has DLLImport linkage, result should be dllimport'ed. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 502 | if (DestIsDeclaration) { |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 503 | LinkFromSrc = true; |
| 504 | LT = Src->getLinkage(); |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 505 | } |
Andrew Lenharth | 8753c44 | 2006-12-15 17:35:32 +0000 | [diff] [blame] | 506 | } else if (Dest->hasExternalWeakLinkage()) { |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 507 | // If the Dest is weak, use the source linkage. |
Andrew Lenharth | 8753c44 | 2006-12-15 17:35:32 +0000 | [diff] [blame] | 508 | LinkFromSrc = true; |
| 509 | LT = Src->getLinkage(); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 510 | } else { |
| 511 | LinkFromSrc = false; |
| 512 | LT = Dest->getLinkage(); |
| 513 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 514 | } else if (DestIsDeclaration && !Dest->hasDLLImportLinkage()) { |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 515 | // If Dest is external but Src is not: |
| 516 | LinkFromSrc = true; |
| 517 | LT = Src->getLinkage(); |
Duncan Sands | a05ef5e | 2009-03-08 13:35:23 +0000 | [diff] [blame] | 518 | } else if (Src->isWeakForLinker()) { |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 519 | // At this point we know that Dest has LinkOnce, External*, Weak, Common, |
| 520 | // or DLL* linkage. |
Chris Lattner | 266c7bb | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 521 | if (Dest->hasExternalWeakLinkage() || |
| 522 | Dest->hasAvailableExternallyLinkage() || |
| 523 | (Dest->hasLinkOnceLinkage() && |
| 524 | (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) { |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 525 | LinkFromSrc = true; |
| 526 | LT = Src->getLinkage(); |
| 527 | } else { |
| 528 | LinkFromSrc = false; |
| 529 | LT = Dest->getLinkage(); |
| 530 | } |
Duncan Sands | a05ef5e | 2009-03-08 13:35:23 +0000 | [diff] [blame] | 531 | } else if (Dest->isWeakForLinker()) { |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 532 | // At this point we know that Src has External* or DLL* linkage. |
| 533 | if (Src->hasExternalWeakLinkage()) { |
| 534 | LinkFromSrc = false; |
| 535 | LT = Dest->getLinkage(); |
| 536 | } else { |
| 537 | LinkFromSrc = true; |
| 538 | LT = GlobalValue::ExternalLinkage; |
| 539 | } |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 540 | } else { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 541 | assert((Dest->hasExternalLinkage() || Dest->hasDLLImportLinkage() || |
| 542 | Dest->hasDLLExportLinkage() || Dest->hasExternalWeakLinkage()) && |
| 543 | (Src->hasExternalLinkage() || Src->hasDLLImportLinkage() || |
| 544 | Src->hasDLLExportLinkage() || Src->hasExternalWeakLinkage()) && |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 545 | "Unexpected linkage type!"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 546 | return emitError("Linking globals named '" + Src->getName() + |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 547 | "': symbol multiply defined!"); |
| 548 | } |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 549 | |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 550 | // Compute the visibility. We follow the rules in the System V Application |
| 551 | // Binary Interface. |
| 552 | Vis = isLessConstraining(Src->getVisibility(), Dest->getVisibility()) ? |
| 553 | Dest->getVisibility() : Src->getVisibility(); |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 554 | return false; |
| 555 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 556 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 557 | /// computeTypeMapping - Loop over all of the linked values to compute type |
| 558 | /// mappings. For example, if we link "extern Foo *x" and "Foo *x = NULL", then |
| 559 | /// we have two struct types 'Foo' but one got renamed when the module was |
| 560 | /// loaded into the same LLVMContext. |
| 561 | void ModuleLinker::computeTypeMapping() { |
| 562 | // Incorporate globals. |
| 563 | for (Module::global_iterator I = SrcM->global_begin(), |
| 564 | E = SrcM->global_end(); I != E; ++I) { |
| 565 | GlobalValue *DGV = getLinkedToGlobal(I); |
| 566 | if (DGV == 0) continue; |
| 567 | |
| 568 | if (!DGV->hasAppendingLinkage() || !I->hasAppendingLinkage()) { |
| 569 | TypeMap.addTypeMapping(DGV->getType(), I->getType()); |
| 570 | continue; |
| 571 | } |
| 572 | |
| 573 | // Unify the element type of appending arrays. |
| 574 | ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType()); |
| 575 | ArrayType *SAT = cast<ArrayType>(I->getType()->getElementType()); |
| 576 | TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType()); |
Devang Patel | ab67e70 | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 577 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 578 | |
| 579 | // Incorporate functions. |
| 580 | for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) { |
| 581 | if (GlobalValue *DGV = getLinkedToGlobal(I)) |
| 582 | TypeMap.addTypeMapping(DGV->getType(), I->getType()); |
| 583 | } |
Bill Wendling | c68d127 | 2012-02-27 22:34:19 +0000 | [diff] [blame] | 584 | |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 585 | // Incorporate types by name, scanning all the types in the source module. |
| 586 | // At this point, the destination module may have a type "%foo = { i32 }" for |
Bill Wendling | 348e5e7 | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 587 | // example. When the source module got loaded into the same LLVMContext, if |
| 588 | // it had the same type, it would have been renamed to "%foo.42 = { i32 }". |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 589 | // Though it isn't required for correctness, attempt to link these up to clean |
| 590 | // up the IR. |
Bill Wendling | 348e5e7 | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 591 | std::vector<StructType*> SrcStructTypes; |
| 592 | SrcM->findUsedStructTypes(SrcStructTypes); |
| 593 | |
| 594 | SmallPtrSet<StructType*, 32> SrcStructTypesSet(SrcStructTypes.begin(), |
| 595 | SrcStructTypes.end()); |
| 596 | |
| 597 | for (unsigned i = 0, e = SrcStructTypes.size(); i != e; ++i) { |
| 598 | StructType *ST = SrcStructTypes[i]; |
| 599 | if (!ST->hasName()) continue; |
| 600 | |
| 601 | // Check to see if there is a dot in the name followed by a digit. |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 602 | size_t DotPos = ST->getName().rfind('.'); |
| 603 | if (DotPos == 0 || DotPos == StringRef::npos || |
| 604 | ST->getName().back() == '.' || !isdigit(ST->getName()[DotPos+1])) |
| 605 | continue; |
Bill Wendling | 348e5e7 | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 606 | |
| 607 | // Check to see if the destination module has a struct with the prefix name. |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 608 | if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos))) |
Bill Wendling | 348e5e7 | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 609 | // Don't use it if this actually came from the source module. They're in |
| 610 | // the same LLVMContext after all. |
| 611 | if (!SrcStructTypesSet.count(DST)) |
| 612 | TypeMap.addTypeMapping(DST, ST); |
| 613 | } |
| 614 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 615 | // Don't bother incorporating aliases, they aren't generally typed well. |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 616 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 617 | // Now that we have discovered all of the type equivalences, get a body for |
| 618 | // any 'opaque' types in the dest module that are now resolved. |
| 619 | TypeMap.linkDefinedTypeBodies(); |
Devang Patel | ab67e70 | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 622 | /// linkAppendingVarProto - If there were any appending global variables, link |
| 623 | /// them together now. Return true on error. |
| 624 | bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV, |
| 625 | GlobalVariable *SrcGV) { |
Bill Wendling | 601c094 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 627 | if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) |
| 628 | return emitError("Linking globals named '" + SrcGV->getName() + |
| 629 | "': can only link appending global with another appending global!"); |
| 630 | |
| 631 | ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType()); |
| 632 | ArrayType *SrcTy = |
| 633 | cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType())); |
| 634 | Type *EltTy = DstTy->getElementType(); |
| 635 | |
| 636 | // Check to see that they two arrays agree on type. |
| 637 | if (EltTy != SrcTy->getElementType()) |
| 638 | return emitError("Appending variables with different element types!"); |
| 639 | if (DstGV->isConstant() != SrcGV->isConstant()) |
| 640 | return emitError("Appending variables linked with different const'ness!"); |
| 641 | |
| 642 | if (DstGV->getAlignment() != SrcGV->getAlignment()) |
| 643 | return emitError( |
| 644 | "Appending variables with different alignment need to be linked!"); |
| 645 | |
| 646 | if (DstGV->getVisibility() != SrcGV->getVisibility()) |
| 647 | return emitError( |
| 648 | "Appending variables with different visibility need to be linked!"); |
| 649 | |
| 650 | if (DstGV->getSection() != SrcGV->getSection()) |
| 651 | return emitError( |
| 652 | "Appending variables with different section name need to be linked!"); |
| 653 | |
| 654 | uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements(); |
| 655 | ArrayType *NewType = ArrayType::get(EltTy, NewSize); |
| 656 | |
| 657 | // Create the new global variable. |
| 658 | GlobalVariable *NG = |
| 659 | new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(), |
| 660 | DstGV->getLinkage(), /*init*/0, /*name*/"", DstGV, |
| 661 | DstGV->isThreadLocal(), |
| 662 | DstGV->getType()->getAddressSpace()); |
| 663 | |
| 664 | // Propagate alignment, visibility and section info. |
| 665 | CopyGVAttributes(NG, DstGV); |
| 666 | |
| 667 | AppendingVarInfo AVI; |
| 668 | AVI.NewGV = NG; |
| 669 | AVI.DstInit = DstGV->getInitializer(); |
| 670 | AVI.SrcInit = SrcGV->getInitializer(); |
| 671 | AppendingVars.push_back(AVI); |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 673 | // Replace any uses of the two global variables with uses of the new |
| 674 | // global. |
| 675 | ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType())); |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 677 | DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType())); |
| 678 | DstGV->eraseFromParent(); |
| 679 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 680 | // Track the source variable so we don't try to link it. |
| 681 | DoNotLinkFromSource.insert(SrcGV); |
| 682 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 683 | return false; |
| 684 | } |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 686 | /// linkGlobalProto - Loop through the global variables in the src module and |
| 687 | /// merge them into the dest module. |
| 688 | bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) { |
| 689 | GlobalValue *DGV = getLinkedToGlobal(SGV); |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 690 | llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility; |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 692 | if (DGV) { |
| 693 | // Concatenation of appending linkage variables is magic and handled later. |
| 694 | if (DGV->hasAppendingLinkage() || SGV->hasAppendingLinkage()) |
| 695 | return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV); |
| 696 | |
| 697 | // Determine whether linkage of these two globals follows the source |
| 698 | // module's definition or the destination module's definition. |
Chris Lattner | b324bd7 | 2006-11-09 05:18:12 +0000 | [diff] [blame] | 699 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 700 | GlobalValue::VisibilityTypes NV; |
Chris Lattner | b324bd7 | 2006-11-09 05:18:12 +0000 | [diff] [blame] | 701 | bool LinkFromSrc = false; |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 702 | if (getLinkageResult(DGV, SGV, NewLinkage, NV, LinkFromSrc)) |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 703 | return true; |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 704 | NewVisibility = NV; |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 705 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 706 | // If we're not linking from the source, then keep the definition that we |
| 707 | // have. |
| 708 | if (!LinkFromSrc) { |
| 709 | // Special case for const propagation. |
| 710 | if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) |
| 711 | if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant()) |
| 712 | DGVar->setConstant(true); |
| 713 | |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 714 | // Set calculated linkage and visibility. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 715 | DGV->setLinkage(NewLinkage); |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 716 | DGV->setVisibility(*NewVisibility); |
| 717 | |
Chris Lattner | 6157e38 | 2008-07-14 07:23:24 +0000 | [diff] [blame] | 718 | // Make sure to remember this mapping. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 719 | ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType())); |
| 720 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 721 | // Track the source global so that we don't attempt to copy it over when |
| 722 | // processing global initializers. |
| 723 | DoNotLinkFromSource.insert(SGV); |
| 724 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 725 | return false; |
Chris Lattner | 6157e38 | 2008-07-14 07:23:24 +0000 | [diff] [blame] | 726 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 727 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 728 | |
| 729 | // No linking to be performed or linking from the source: simply create an |
| 730 | // identical version of the symbol over in the dest module... the |
| 731 | // initializer will be filled in later by LinkGlobalInits. |
| 732 | GlobalVariable *NewDGV = |
| 733 | new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()), |
| 734 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
| 735 | SGV->getName(), /*insertbefore*/0, |
| 736 | SGV->isThreadLocal(), |
| 737 | SGV->getType()->getAddressSpace()); |
| 738 | // Propagate alignment, visibility and section info. |
| 739 | CopyGVAttributes(NewDGV, SGV); |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 740 | if (NewVisibility) |
| 741 | NewDGV->setVisibility(*NewVisibility); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 742 | |
| 743 | if (DGV) { |
| 744 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType())); |
| 745 | DGV->eraseFromParent(); |
| 746 | } |
| 747 | |
| 748 | // Make sure to remember this mapping. |
| 749 | ValueMap[SGV] = NewDGV; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 750 | return false; |
| 751 | } |
| 752 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 753 | /// linkFunctionProto - Link the function in the source module into the |
| 754 | /// destination module if needed, setting up mapping information. |
| 755 | bool ModuleLinker::linkFunctionProto(Function *SF) { |
| 756 | GlobalValue *DGV = getLinkedToGlobal(SF); |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 757 | llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 758 | |
| 759 | if (DGV) { |
| 760 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 761 | bool LinkFromSrc = false; |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 762 | GlobalValue::VisibilityTypes NV; |
| 763 | if (getLinkageResult(DGV, SF, NewLinkage, NV, LinkFromSrc)) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 764 | return true; |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 765 | NewVisibility = NV; |
| 766 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 767 | if (!LinkFromSrc) { |
| 768 | // Set calculated linkage |
| 769 | DGV->setLinkage(NewLinkage); |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 770 | DGV->setVisibility(*NewVisibility); |
| 771 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 772 | // Make sure to remember this mapping. |
| 773 | ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType())); |
| 774 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 775 | // Track the function from the source module so we don't attempt to remap |
| 776 | // it. |
| 777 | DoNotLinkFromSource.insert(SF); |
| 778 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 779 | return false; |
| 780 | } |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 781 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 782 | |
| 783 | // If there is no linkage to be performed or we are linking from the source, |
| 784 | // bring SF over. |
| 785 | Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()), |
| 786 | SF->getLinkage(), SF->getName(), DstM); |
| 787 | CopyGVAttributes(NewDF, SF); |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 788 | if (NewVisibility) |
| 789 | NewDF->setVisibility(*NewVisibility); |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 791 | if (DGV) { |
| 792 | // Any uses of DF need to change to NewDF, with cast. |
| 793 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType())); |
| 794 | DGV->eraseFromParent(); |
Tanya Lattner | 9af37a3 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 795 | } else { |
| 796 | // Internal, LO_ODR, or LO linkage - stick in set to ignore and lazily link. |
| 797 | if (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() || |
| 798 | SF->hasAvailableExternallyLinkage()) { |
| 799 | DoNotLinkFromSource.insert(SF); |
| 800 | LazilyLinkFunctions.push_back(SF); |
| 801 | } |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 802 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 803 | |
| 804 | ValueMap[SF] = NewDF; |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 805 | return false; |
| 806 | } |
| 807 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 808 | /// LinkAliasProto - Set up prototypes for any aliases that come over from the |
| 809 | /// source module. |
| 810 | bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) { |
| 811 | GlobalValue *DGV = getLinkedToGlobal(SGA); |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 812 | llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility; |
| 813 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 814 | if (DGV) { |
| 815 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 816 | GlobalValue::VisibilityTypes NV; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 817 | bool LinkFromSrc = false; |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 818 | if (getLinkageResult(DGV, SGA, NewLinkage, NV, LinkFromSrc)) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 819 | return true; |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 820 | NewVisibility = NV; |
| 821 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 822 | if (!LinkFromSrc) { |
| 823 | // Set calculated linkage. |
| 824 | DGV->setLinkage(NewLinkage); |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 825 | DGV->setVisibility(*NewVisibility); |
| 826 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 827 | // Make sure to remember this mapping. |
| 828 | ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType())); |
| 829 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 830 | // Track the alias from the source module so we don't attempt to remap it. |
| 831 | DoNotLinkFromSource.insert(SGA); |
| 832 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 833 | return false; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | // If there is no linkage to be performed or we're linking from the source, |
| 838 | // bring over SGA. |
| 839 | GlobalAlias *NewDA = new GlobalAlias(TypeMap.get(SGA->getType()), |
| 840 | SGA->getLinkage(), SGA->getName(), |
| 841 | /*aliasee*/0, DstM); |
| 842 | CopyGVAttributes(NewDA, SGA); |
Rafael Espindola | 3ed8815 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 843 | if (NewVisibility) |
| 844 | NewDA->setVisibility(*NewVisibility); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 846 | if (DGV) { |
| 847 | // Any uses of DGV need to change to NewDA, with cast. |
| 848 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType())); |
| 849 | DGV->eraseFromParent(); |
| 850 | } |
| 851 | |
| 852 | ValueMap[SGA] = NewDA; |
| 853 | return false; |
| 854 | } |
| 855 | |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 856 | static void getArrayElements(Constant *C, SmallVectorImpl<Constant*> &Dest) { |
Chris Lattner | a1f00f4 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 857 | unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements(); |
| 858 | |
| 859 | for (unsigned i = 0; i != NumElements; ++i) |
| 860 | Dest.push_back(C->getAggregateElement(i)); |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 863 | void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) { |
| 864 | // Merge the initializer. |
| 865 | SmallVector<Constant*, 16> Elements; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 866 | getArrayElements(AVI.DstInit, Elements); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 867 | |
| 868 | Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap); |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 869 | getArrayElements(SrcInit, Elements); |
| 870 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 871 | ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType()); |
| 872 | AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements)); |
| 873 | } |
| 874 | |
| 875 | |
| 876 | // linkGlobalInits - Update the initializers in the Dest module now that all |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 877 | // globals that may be referenced are in Dest. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 878 | void ModuleLinker::linkGlobalInits() { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 879 | // Loop over all of the globals in the src module, mapping them over as we go |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 880 | for (Module::const_global_iterator I = SrcM->global_begin(), |
| 881 | E = SrcM->global_end(); I != E; ++I) { |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 882 | |
| 883 | // Only process initialized GV's or ones not already in dest. |
| 884 | if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 885 | |
| 886 | // Grab destination global variable. |
| 887 | GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]); |
| 888 | // Figure out what the initializer looks like in the dest module. |
| 889 | DGV->setInitializer(MapValue(I->getInitializer(), ValueMap, |
| 890 | RF_None, &TypeMap)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 891 | } |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 892 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 893 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 894 | // linkFunctionBody - Copy the source function over into the dest function and |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 895 | // fix up references to values. At this point we know that Dest is an external |
| 896 | // function, and that Src is not. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 897 | void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) { |
| 898 | assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 899 | |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 900 | // Go through and convert function arguments over, remembering the mapping. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 901 | Function::arg_iterator DI = Dst->arg_begin(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 902 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 903 | I != E; ++I, ++DI) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 904 | DI->setName(I->getName()); // Copy the name over. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 906 | // Add a mapping to our mapping. |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 907 | ValueMap[I] = DI; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 910 | if (Mode == Linker::DestroySource) { |
| 911 | // Splice the body of the source function into the dest function. |
| 912 | Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList()); |
| 913 | |
| 914 | // At this point, all of the instructions and values of the function are now |
| 915 | // copied over. The only problem is that they are still referencing values in |
| 916 | // the Source function as operands. Loop through all of the operands of the |
| 917 | // functions and patch them up to point to the local versions. |
| 918 | for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB) |
| 919 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 920 | RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, &TypeMap); |
| 921 | |
| 922 | } else { |
| 923 | // Clone the body of the function into the dest function. |
| 924 | SmallVector<ReturnInst*, 8> Returns; // Ignore returns. |
Mon P Wang | d24397a | 2011-12-23 02:18:32 +0000 | [diff] [blame] | 925 | CloneFunctionInto(Dst, Src, ValueMap, false, Returns, "", NULL, &TypeMap); |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 928 | // There is no need to map the arguments anymore. |
Chris Lattner | 1127315 | 2006-06-16 01:24:04 +0000 | [diff] [blame] | 929 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
| 930 | I != E; ++I) |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 931 | ValueMap.erase(I); |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 936 | void ModuleLinker::linkAliasBodies() { |
| 937 | for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end(); |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 938 | I != E; ++I) { |
| 939 | if (DoNotLinkFromSource.count(I)) |
| 940 | continue; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 941 | if (Constant *Aliasee = I->getAliasee()) { |
| 942 | GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]); |
| 943 | DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None, &TypeMap)); |
David Chisnall | 3472246 | 2010-01-09 16:27:31 +0000 | [diff] [blame] | 944 | } |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 945 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 946 | } |
Anton Korobeynikov | 9f2ee70 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 947 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 948 | /// linkNamedMDNodes - Insert all of the named mdnodes in Src into the Dest |
| 949 | /// module. |
| 950 | void ModuleLinker::linkNamedMDNodes() { |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 951 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 952 | for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(), |
| 953 | E = SrcM->named_metadata_end(); I != E; ++I) { |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 954 | // Don't link module flags here. Do them separately. |
| 955 | if (&*I == SrcModFlags) continue; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 956 | NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName()); |
| 957 | // Add Src elements into Dest node. |
| 958 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 959 | DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap, |
| 960 | RF_None, &TypeMap)); |
| 961 | } |
| 962 | } |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 963 | |
| 964 | /// categorizeModuleFlagNodes - |
| 965 | bool ModuleLinker:: |
| 966 | categorizeModuleFlagNodes(const NamedMDNode *ModFlags, |
| 967 | DenseMap<MDString*, MDNode*> &ErrorNode, |
| 968 | DenseMap<MDString*, MDNode*> &WarningNode, |
| 969 | DenseMap<MDString*, MDNode*> &OverrideNode, |
| 970 | DenseMap<MDString*, |
| 971 | SmallSetVector<MDNode*, 8> > &RequireNodes, |
| 972 | SmallSetVector<MDString*, 16> &SeenIDs) { |
| 973 | bool HasErr = false; |
| 974 | |
| 975 | for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) { |
| 976 | MDNode *Op = ModFlags->getOperand(I); |
| 977 | assert(Op->getNumOperands() == 3 && "Invalid module flag metadata!"); |
| 978 | assert(isa<ConstantInt>(Op->getOperand(0)) && |
| 979 | "Module flag's first operand must be an integer!"); |
| 980 | assert(isa<MDString>(Op->getOperand(1)) && |
| 981 | "Module flag's second operand must be an MDString!"); |
| 982 | |
| 983 | ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0)); |
| 984 | MDString *ID = cast<MDString>(Op->getOperand(1)); |
| 985 | Value *Val = Op->getOperand(2); |
| 986 | switch (Behavior->getZExtValue()) { |
| 987 | default: |
| 988 | assert(false && "Invalid behavior in module flag metadata!"); |
| 989 | break; |
| 990 | case Module::Error: { |
| 991 | MDNode *&ErrNode = ErrorNode[ID]; |
| 992 | if (!ErrNode) ErrNode = Op; |
| 993 | if (ErrNode->getOperand(2) != Val) |
Bill Wendling | 75b3d68 | 2012-02-14 09:13:54 +0000 | [diff] [blame] | 994 | HasErr = emitError("linking module flags '" + ID->getString() + |
| 995 | "': IDs have conflicting values"); |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 996 | break; |
| 997 | } |
| 998 | case Module::Warning: { |
| 999 | MDNode *&WarnNode = WarningNode[ID]; |
| 1000 | if (!WarnNode) WarnNode = Op; |
| 1001 | if (WarnNode->getOperand(2) != Val) |
Bill Wendling | 75b3d68 | 2012-02-14 09:13:54 +0000 | [diff] [blame] | 1002 | errs() << "WARNING: linking module flags '" << ID->getString() |
| 1003 | << "': IDs have conflicting values"; |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1004 | break; |
| 1005 | } |
| 1006 | case Module::Require: RequireNodes[ID].insert(Op); break; |
| 1007 | case Module::Override: { |
| 1008 | MDNode *&OvrNode = OverrideNode[ID]; |
| 1009 | if (!OvrNode) OvrNode = Op; |
| 1010 | if (OvrNode->getOperand(2) != Val) |
Bill Wendling | 75b3d68 | 2012-02-14 09:13:54 +0000 | [diff] [blame] | 1011 | HasErr = emitError("linking module flags '" + ID->getString() + |
| 1012 | "': IDs have conflicting override values"); |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | SeenIDs.insert(ID); |
| 1018 | } |
| 1019 | |
| 1020 | return HasErr; |
| 1021 | } |
| 1022 | |
| 1023 | /// linkModuleFlagsMetadata - Merge the linker flags in Src into the Dest |
| 1024 | /// module. |
| 1025 | bool ModuleLinker::linkModuleFlagsMetadata() { |
| 1026 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
| 1027 | if (!SrcModFlags) return false; |
| 1028 | |
| 1029 | NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata(); |
| 1030 | |
| 1031 | // If the destination module doesn't have module flags yet, then just copy |
| 1032 | // over the source module's flags. |
| 1033 | if (DstModFlags->getNumOperands() == 0) { |
| 1034 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) |
| 1035 | DstModFlags->addOperand(SrcModFlags->getOperand(I)); |
| 1036 | |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
| 1040 | bool HasErr = false; |
| 1041 | |
| 1042 | // Otherwise, we have to merge them based on their behaviors. First, |
| 1043 | // categorize all of the nodes in the modules' module flags. If an error or |
| 1044 | // warning occurs, then emit the appropriate message(s). |
| 1045 | DenseMap<MDString*, MDNode*> ErrorNode; |
| 1046 | DenseMap<MDString*, MDNode*> WarningNode; |
| 1047 | DenseMap<MDString*, MDNode*> OverrideNode; |
| 1048 | DenseMap<MDString*, SmallSetVector<MDNode*, 8> > RequireNodes; |
| 1049 | SmallSetVector<MDString*, 16> SeenIDs; |
| 1050 | |
| 1051 | HasErr |= categorizeModuleFlagNodes(SrcModFlags, ErrorNode, WarningNode, |
| 1052 | OverrideNode, RequireNodes, SeenIDs); |
| 1053 | HasErr |= categorizeModuleFlagNodes(DstModFlags, ErrorNode, WarningNode, |
| 1054 | OverrideNode, RequireNodes, SeenIDs); |
| 1055 | |
| 1056 | // Check that there isn't both an error and warning node for a flag. |
| 1057 | for (SmallSetVector<MDString*, 16>::iterator |
| 1058 | I = SeenIDs.begin(), E = SeenIDs.end(); I != E; ++I) { |
| 1059 | MDString *ID = *I; |
| 1060 | if (ErrorNode[ID] && WarningNode[ID]) |
Bill Wendling | 75b3d68 | 2012-02-14 09:13:54 +0000 | [diff] [blame] | 1061 | HasErr = emitError("linking module flags '" + ID->getString() + |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1062 | "': IDs have conflicting behaviors"); |
| 1063 | } |
| 1064 | |
| 1065 | // Early exit if we had an error. |
| 1066 | if (HasErr) return true; |
| 1067 | |
| 1068 | // Get the destination's module flags ready for new operands. |
| 1069 | DstModFlags->dropAllReferences(); |
| 1070 | |
| 1071 | // Add all of the module flags to the destination module. |
| 1072 | DenseMap<MDString*, SmallVector<MDNode*, 4> > AddedNodes; |
| 1073 | for (SmallSetVector<MDString*, 16>::iterator |
| 1074 | I = SeenIDs.begin(), E = SeenIDs.end(); I != E; ++I) { |
| 1075 | MDString *ID = *I; |
| 1076 | if (OverrideNode[ID]) { |
| 1077 | DstModFlags->addOperand(OverrideNode[ID]); |
| 1078 | AddedNodes[ID].push_back(OverrideNode[ID]); |
| 1079 | } else if (ErrorNode[ID]) { |
| 1080 | DstModFlags->addOperand(ErrorNode[ID]); |
| 1081 | AddedNodes[ID].push_back(ErrorNode[ID]); |
| 1082 | } else if (WarningNode[ID]) { |
| 1083 | DstModFlags->addOperand(WarningNode[ID]); |
| 1084 | AddedNodes[ID].push_back(WarningNode[ID]); |
| 1085 | } |
| 1086 | |
| 1087 | for (SmallSetVector<MDNode*, 8>::iterator |
| 1088 | II = RequireNodes[ID].begin(), IE = RequireNodes[ID].end(); |
| 1089 | II != IE; ++II) |
| 1090 | DstModFlags->addOperand(*II); |
| 1091 | } |
| 1092 | |
| 1093 | // Now check that all of the requirements have been satisfied. |
| 1094 | for (SmallSetVector<MDString*, 16>::iterator |
| 1095 | I = SeenIDs.begin(), E = SeenIDs.end(); I != E; ++I) { |
| 1096 | MDString *ID = *I; |
| 1097 | SmallSetVector<MDNode*, 8> &Set = RequireNodes[ID]; |
| 1098 | |
| 1099 | for (SmallSetVector<MDNode*, 8>::iterator |
| 1100 | II = Set.begin(), IE = Set.end(); II != IE; ++II) { |
| 1101 | MDNode *Node = *II; |
| 1102 | assert(isa<MDNode>(Node->getOperand(2)) && |
| 1103 | "Module flag's third operand must be an MDNode!"); |
| 1104 | MDNode *Val = cast<MDNode>(Node->getOperand(2)); |
| 1105 | |
| 1106 | MDString *ReqID = cast<MDString>(Val->getOperand(0)); |
| 1107 | Value *ReqVal = Val->getOperand(1); |
| 1108 | |
| 1109 | bool HasValue = false; |
| 1110 | for (SmallVectorImpl<MDNode*>::iterator |
| 1111 | RI = AddedNodes[ReqID].begin(), RE = AddedNodes[ReqID].end(); |
| 1112 | RI != RE; ++RI) { |
| 1113 | MDNode *ReqNode = *RI; |
| 1114 | if (ReqNode->getOperand(2) == ReqVal) { |
| 1115 | HasValue = true; |
| 1116 | break; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | if (!HasValue) |
Bill Wendling | 75b3d68 | 2012-02-14 09:13:54 +0000 | [diff] [blame] | 1121 | HasErr = emitError("linking module flags '" + ReqID->getString() + |
| 1122 | "': does not have the required value"); |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | return HasErr; |
| 1127 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1128 | |
| 1129 | bool ModuleLinker::run() { |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1130 | assert(DstM && "Null destination module"); |
| 1131 | assert(SrcM && "Null source module"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Inherit the target data from the source module if the destination module |
| 1134 | // doesn't have one already. |
| 1135 | if (DstM->getDataLayout().empty() && !SrcM->getDataLayout().empty()) |
| 1136 | DstM->setDataLayout(SrcM->getDataLayout()); |
| 1137 | |
| 1138 | // Copy the target triple from the source to dest if the dest's is empty. |
| 1139 | if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty()) |
| 1140 | DstM->setTargetTriple(SrcM->getTargetTriple()); |
| 1141 | |
| 1142 | if (!SrcM->getDataLayout().empty() && !DstM->getDataLayout().empty() && |
| 1143 | SrcM->getDataLayout() != DstM->getDataLayout()) |
| 1144 | errs() << "WARNING: Linking two modules of different data layouts!\n"; |
| 1145 | if (!SrcM->getTargetTriple().empty() && |
| 1146 | DstM->getTargetTriple() != SrcM->getTargetTriple()) { |
| 1147 | errs() << "WARNING: Linking two modules of different target triples: "; |
| 1148 | if (!SrcM->getModuleIdentifier().empty()) |
| 1149 | errs() << SrcM->getModuleIdentifier() << ": "; |
| 1150 | errs() << "'" << SrcM->getTargetTriple() << "' and '" |
| 1151 | << DstM->getTargetTriple() << "'\n"; |
| 1152 | } |
| 1153 | |
| 1154 | // Append the module inline asm string. |
| 1155 | if (!SrcM->getModuleInlineAsm().empty()) { |
| 1156 | if (DstM->getModuleInlineAsm().empty()) |
| 1157 | DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm()); |
| 1158 | else |
| 1159 | DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+ |
| 1160 | SrcM->getModuleInlineAsm()); |
| 1161 | } |
| 1162 | |
| 1163 | // Update the destination module's dependent libraries list with the libraries |
| 1164 | // from the source module. There's no opportunity for duplicates here as the |
| 1165 | // Module ensures that duplicate insertions are discarded. |
| 1166 | for (Module::lib_iterator SI = SrcM->lib_begin(), SE = SrcM->lib_end(); |
| 1167 | SI != SE; ++SI) |
| 1168 | DstM->addLibrary(*SI); |
| 1169 | |
| 1170 | // If the source library's module id is in the dependent library list of the |
| 1171 | // destination library, remove it since that module is now linked in. |
| 1172 | StringRef ModuleId = SrcM->getModuleIdentifier(); |
| 1173 | if (!ModuleId.empty()) |
| 1174 | DstM->removeLibrary(sys::path::stem(ModuleId)); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Loop over all of the linked values to compute type mappings. |
| 1177 | computeTypeMapping(); |
| 1178 | |
| 1179 | // Insert all of the globals in src into the DstM module... without linking |
| 1180 | // initializers (which could refer to functions not yet mapped over). |
| 1181 | for (Module::global_iterator I = SrcM->global_begin(), |
| 1182 | E = SrcM->global_end(); I != E; ++I) |
| 1183 | if (linkGlobalProto(I)) |
| 1184 | return true; |
| 1185 | |
| 1186 | // Link the functions together between the two modules, without doing function |
| 1187 | // bodies... this just adds external function prototypes to the DstM |
| 1188 | // function... We do this so that when we begin processing function bodies, |
| 1189 | // all of the global values that may be referenced are available in our |
| 1190 | // ValueMap. |
| 1191 | for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) |
| 1192 | if (linkFunctionProto(I)) |
| 1193 | return true; |
| 1194 | |
| 1195 | // If there were any aliases, link them now. |
| 1196 | for (Module::alias_iterator I = SrcM->alias_begin(), |
| 1197 | E = SrcM->alias_end(); I != E; ++I) |
| 1198 | if (linkAliasProto(I)) |
| 1199 | return true; |
| 1200 | |
| 1201 | for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i) |
| 1202 | linkAppendingVarInit(AppendingVars[i]); |
| 1203 | |
| 1204 | // Update the initializers in the DstM module now that all globals that may |
| 1205 | // be referenced are in DstM. |
| 1206 | linkGlobalInits(); |
| 1207 | |
| 1208 | // Link in the function bodies that are defined in the source module into |
| 1209 | // DstM. |
| 1210 | for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) { |
Tanya Lattner | 2b28a74 | 2011-10-14 22:17:46 +0000 | [diff] [blame] | 1211 | // Skip if not linking from source. |
| 1212 | if (DoNotLinkFromSource.count(SF)) continue; |
| 1213 | |
| 1214 | // Skip if no body (function is external) or materialize. |
| 1215 | if (SF->isDeclaration()) { |
| 1216 | if (!SF->isMaterializable()) |
| 1217 | continue; |
| 1218 | if (SF->Materialize(&ErrorMsg)) |
| 1219 | return true; |
| 1220 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1221 | |
| 1222 | linkFunctionBody(cast<Function>(ValueMap[SF]), SF); |
| 1223 | } |
| 1224 | |
| 1225 | // Resolve all uses of aliases with aliasees. |
| 1226 | linkAliasBodies(); |
| 1227 | |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1228 | // Remap all of the named MDNodes in Src into the DstM module. We do this |
Devang Patel | 211da8f | 2011-08-04 19:44:28 +0000 | [diff] [blame] | 1229 | // after linking GlobalValues so that MDNodes that reference GlobalValues |
| 1230 | // are properly remapped. |
| 1231 | linkNamedMDNodes(); |
| 1232 | |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1233 | // Merge the module flags into the DstM module. |
| 1234 | if (linkModuleFlagsMetadata()) |
| 1235 | return true; |
| 1236 | |
Tanya Lattner | 9af37a3 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 1237 | // Process vector of lazily linked in functions. |
| 1238 | bool LinkedInAnyFunctions; |
| 1239 | do { |
| 1240 | LinkedInAnyFunctions = false; |
| 1241 | |
| 1242 | for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(), |
| 1243 | E = LazilyLinkFunctions.end(); I != E; ++I) { |
| 1244 | if (!*I) |
| 1245 | continue; |
| 1246 | |
| 1247 | Function *SF = *I; |
| 1248 | Function *DF = cast<Function>(ValueMap[SF]); |
| 1249 | |
| 1250 | if (!DF->use_empty()) { |
| 1251 | |
| 1252 | // Materialize if necessary. |
| 1253 | if (SF->isDeclaration()) { |
| 1254 | if (!SF->isMaterializable()) |
| 1255 | continue; |
| 1256 | if (SF->Materialize(&ErrorMsg)) |
| 1257 | return true; |
| 1258 | } |
| 1259 | |
| 1260 | // Link in function body. |
| 1261 | linkFunctionBody(DF, SF); |
| 1262 | |
| 1263 | // "Remove" from vector by setting the element to 0. |
| 1264 | *I = 0; |
| 1265 | |
| 1266 | // Set flag to indicate we may have more functions to lazily link in |
| 1267 | // since we linked in a function. |
| 1268 | LinkedInAnyFunctions = true; |
| 1269 | } |
| 1270 | } |
| 1271 | } while (LinkedInAnyFunctions); |
| 1272 | |
| 1273 | // Remove any prototypes of functions that were not actually linked in. |
| 1274 | for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(), |
| 1275 | E = LazilyLinkFunctions.end(); I != E; ++I) { |
| 1276 | if (!*I) |
| 1277 | continue; |
| 1278 | |
| 1279 | Function *SF = *I; |
| 1280 | Function *DF = cast<Function>(ValueMap[SF]); |
| 1281 | if (DF->use_empty()) |
| 1282 | DF->eraseFromParent(); |
| 1283 | } |
| 1284 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1285 | // Now that all of the types from the source are used, resolve any structs |
| 1286 | // copied over to the dest that didn't exist there. |
| 1287 | TypeMap.linkDefinedTypeBodies(); |
| 1288 | |
Anton Korobeynikov | 9f2ee70 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1289 | return false; |
| 1290 | } |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1292 | //===----------------------------------------------------------------------===// |
| 1293 | // LinkModules entrypoint. |
| 1294 | //===----------------------------------------------------------------------===// |
| 1295 | |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1296 | // LinkModules - This function links two modules together, with the resulting |
| 1297 | // left module modified to be the composite of the two input modules. If an |
| 1298 | // error occurs, true is returned and ErrorMsg (if not null) is set to indicate |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1299 | // the problem. Upon failure, the Dest module could be in a modified state, and |
| 1300 | // shouldn't be relied on to be consistent. |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1301 | bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode, |
| 1302 | std::string *ErrorMsg) { |
| 1303 | ModuleLinker TheLinker(Dest, Src, Mode); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1304 | if (TheLinker.run()) { |
| 1305 | if (ErrorMsg) *ErrorMsg = TheLinker.ErrorMsg; |
Reid Spencer | 619f024 | 2007-02-04 04:43:17 +0000 | [diff] [blame] | 1306 | return true; |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 1307 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1308 | |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1309 | return false; |
| 1310 | } |