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