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