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