Reid Spencer | 7f49602 | 2004-11-12 20:37:43 +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 | // |
| 12 | // Specifically, this: |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 13 | // * Merges global variables between the two modules |
| 14 | // * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if != |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 15 | // * Merges functions between two modules |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Reid Spencer | 7cc371a | 2004-11-14 23:27:04 +0000 | [diff] [blame] | 19 | #include "llvm/Linker.h" |
Chris Lattner | adbc0b5 | 2003-11-20 18:23:14 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 23 | #include "llvm/TypeSymbolTable.h" |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 24 | #include "llvm/ValueSymbolTable.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
Chris Lattner | adbc0b5 | 2003-11-20 18:23:14 +0000 | [diff] [blame] | 26 | #include "llvm/Assembly/Writer.h" |
Bill Wendling | 41edad7 | 2006-11-27 10:09:12 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Streams.h" |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 28 | #include "llvm/System/Path.h" |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
Bill Wendling | 1a097e3 | 2006-12-07 23:41:45 +0000 | [diff] [blame] | 30 | #include <sstream> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 33 | // Error - Simple wrapper function to conditionally assign to E and return true. |
| 34 | // This just makes error return conditions a little bit simpler... |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 35 | static inline bool Error(std::string *E, const std::string &Message) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 36 | if (E) *E = Message; |
| 37 | return true; |
| 38 | } |
| 39 | |
Reid Spencer | 719012d | 2004-11-25 09:29:44 +0000 | [diff] [blame] | 40 | // ToStr - Simple wrapper function to convert a type to a string. |
Chris Lattner | 72cf7df | 2004-08-21 00:50:59 +0000 | [diff] [blame] | 41 | static std::string ToStr(const Type *Ty, const Module *M) { |
| 42 | std::ostringstream OS; |
| 43 | WriteTypeSymbolic(OS, Ty, M); |
| 44 | return OS.str(); |
| 45 | } |
| 46 | |
John Criswell | 700867b | 2003-11-04 15:22:26 +0000 | [diff] [blame] | 47 | // |
| 48 | // Function: ResolveTypes() |
| 49 | // |
| 50 | // Description: |
| 51 | // Attempt to link the two specified types together. |
| 52 | // |
| 53 | // Inputs: |
| 54 | // DestTy - The type to which we wish to resolve. |
| 55 | // SrcTy - The original type which we want to resolve. |
John Criswell | 700867b | 2003-11-04 15:22:26 +0000 | [diff] [blame] | 56 | // |
| 57 | // Outputs: |
| 58 | // DestST - The symbol table in which the new type should be placed. |
| 59 | // |
| 60 | // Return value: |
| 61 | // true - There is an error and the types cannot yet be linked. |
| 62 | // false - No errors. |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 63 | // |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 64 | static bool ResolveTypes(const Type *DestTy, const Type *SrcTy) { |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 65 | if (DestTy == SrcTy) return false; // If already equal, noop |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 66 | assert(DestTy && SrcTy && "Can't handle null types"); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 67 | |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 68 | if (const OpaqueType *OT = dyn_cast<OpaqueType>(DestTy)) { |
| 69 | // Type _is_ in module, just opaque... |
| 70 | const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(SrcTy); |
| 71 | } else if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) { |
| 72 | const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy); |
| 73 | } else { |
| 74 | return true; // Cannot link types... not-equal and neither is opaque. |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 79 | /// LinkerTypeMap - This implements a map of types that is stable |
| 80 | /// even if types are resolved/refined to other types. This is not a general |
| 81 | /// purpose map, it is specific to the linker's use. |
| 82 | namespace { |
| 83 | class LinkerTypeMap : public AbstractTypeUser { |
| 84 | typedef DenseMap<const Type*, PATypeHolder> TheMapTy; |
| 85 | TheMapTy TheMap; |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 86 | |
Chris Lattner | fc196f9 | 2008-06-16 23:06:51 +0000 | [diff] [blame] | 87 | LinkerTypeMap(const LinkerTypeMap&); // DO NOT IMPLEMENT |
| 88 | void operator=(const LinkerTypeMap&); // DO NOT IMPLEMENT |
| 89 | public: |
| 90 | LinkerTypeMap() {} |
| 91 | ~LinkerTypeMap() { |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 92 | for (DenseMap<const Type*, PATypeHolder>::iterator I = TheMap.begin(), |
| 93 | E = TheMap.end(); I != E; ++I) |
| 94 | I->first->removeAbstractTypeUser(this); |
| 95 | } |
| 96 | |
| 97 | /// lookup - Return the value for the specified type or null if it doesn't |
| 98 | /// exist. |
| 99 | const Type *lookup(const Type *Ty) const { |
| 100 | TheMapTy::const_iterator I = TheMap.find(Ty); |
| 101 | if (I != TheMap.end()) return I->second; |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /// erase - Remove the specified type, returning true if it was in the set. |
| 106 | bool erase(const Type *Ty) { |
| 107 | if (!TheMap.erase(Ty)) |
| 108 | return false; |
| 109 | if (Ty->isAbstract()) |
| 110 | Ty->removeAbstractTypeUser(this); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | /// insert - This returns true if the pointer was new to the set, false if it |
| 115 | /// was already in the set. |
| 116 | bool insert(const Type *Src, const Type *Dst) { |
| 117 | if (!TheMap.insert(std::make_pair(Src, PATypeHolder(Dst)))) |
| 118 | return false; // Already in map. |
| 119 | if (Src->isAbstract()) |
| 120 | Src->addAbstractTypeUser(this); |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | protected: |
| 125 | /// refineAbstractType - The callback method invoked when an abstract type is |
| 126 | /// resolved to another type. An object must override this method to update |
| 127 | /// its internal state to reference NewType instead of OldType. |
| 128 | /// |
| 129 | virtual void refineAbstractType(const DerivedType *OldTy, |
| 130 | const Type *NewTy) { |
| 131 | TheMapTy::iterator I = TheMap.find(OldTy); |
| 132 | const Type *DstTy = I->second; |
| 133 | |
| 134 | TheMap.erase(I); |
| 135 | if (OldTy->isAbstract()) |
| 136 | OldTy->removeAbstractTypeUser(this); |
| 137 | |
| 138 | // Don't reinsert into the map if the key is concrete now. |
| 139 | if (NewTy->isAbstract()) |
| 140 | insert(NewTy, DstTy); |
| 141 | } |
| 142 | |
| 143 | /// The other case which AbstractTypeUsers must be aware of is when a type |
| 144 | /// makes the transition from being abstract (where it has clients on it's |
| 145 | /// AbstractTypeUsers list) to concrete (where it does not). This method |
| 146 | /// notifies ATU's when this occurs for a type. |
| 147 | virtual void typeBecameConcrete(const DerivedType *AbsTy) { |
| 148 | TheMap.erase(AbsTy); |
| 149 | AbsTy->removeAbstractTypeUser(this); |
| 150 | } |
| 151 | |
| 152 | // for debugging... |
| 153 | virtual void dump() const { |
| 154 | cerr << "AbstractTypeSet!\n"; |
| 155 | } |
| 156 | }; |
| 157 | } |
| 158 | |
| 159 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 160 | // RecursiveResolveTypes - This is just like ResolveTypes, except that it |
| 161 | // recurses down into derived types, merging the used types if the parent types |
| 162 | // are compatible. |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 163 | static bool RecursiveResolveTypesI(const Type *DstTy, const Type *SrcTy, |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 164 | LinkerTypeMap &Pointers) { |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 165 | if (DstTy == SrcTy) return false; // If already equal, noop |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 166 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 167 | // If we found our opaque type, resolve it now! |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 168 | if (isa<OpaqueType>(DstTy) || isa<OpaqueType>(SrcTy)) |
| 169 | return ResolveTypes(DstTy, SrcTy); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 170 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 171 | // Two types cannot be resolved together if they are of different primitive |
| 172 | // type. For example, we cannot resolve an int to a float. |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 173 | if (DstTy->getTypeID() != SrcTy->getTypeID()) return true; |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 5653965 | 2008-06-16 20:03:01 +0000 | [diff] [blame] | 175 | // If neither type is abstract, then they really are just different types. |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 176 | if (!DstTy->isAbstract() && !SrcTy->isAbstract()) |
Chris Lattner | 5653965 | 2008-06-16 20:03:01 +0000 | [diff] [blame] | 177 | return true; |
| 178 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 179 | // Otherwise, resolve the used type used by this derived type... |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 180 | switch (DstTy->getTypeID()) { |
Chris Lattner | f6f4f7a | 2008-06-16 18:27:53 +0000 | [diff] [blame] | 181 | default: |
| 182 | return true; |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 183 | case Type::FunctionTyID: { |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 184 | const FunctionType *DstFT = cast<FunctionType>(DstTy); |
| 185 | const FunctionType *SrcFT = cast<FunctionType>(SrcTy); |
Chris Lattner | 9ddf2c8 | 2008-06-16 19:55:40 +0000 | [diff] [blame] | 186 | if (DstFT->isVarArg() != SrcFT->isVarArg() || |
| 187 | DstFT->getNumContainedTypes() != SrcFT->getNumContainedTypes()) |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 188 | return true; |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 189 | |
| 190 | // Use TypeHolder's so recursive resolution won't break us. |
| 191 | PATypeHolder ST(SrcFT), DT(DstFT); |
| 192 | for (unsigned i = 0, e = DstFT->getNumContainedTypes(); i != e; ++i) { |
| 193 | const Type *SE = ST->getContainedType(i), *DE = DT->getContainedType(i); |
| 194 | if (SE != DE && RecursiveResolveTypesI(DE, SE, Pointers)) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 195 | return true; |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | case Type::StructTyID: { |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 200 | const StructType *DstST = cast<StructType>(DstTy); |
| 201 | const StructType *SrcST = cast<StructType>(SrcTy); |
Chris Lattner | 9ddf2c8 | 2008-06-16 19:55:40 +0000 | [diff] [blame] | 202 | if (DstST->getNumContainedTypes() != SrcST->getNumContainedTypes()) |
Chris Lattner | f6f4f7a | 2008-06-16 18:27:53 +0000 | [diff] [blame] | 203 | return true; |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 204 | |
| 205 | PATypeHolder ST(SrcST), DT(DstST); |
| 206 | for (unsigned i = 0, e = DstST->getNumContainedTypes(); i != e; ++i) { |
| 207 | const Type *SE = ST->getContainedType(i), *DE = DT->getContainedType(i); |
| 208 | if (SE != DE && RecursiveResolveTypesI(DE, SE, Pointers)) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 209 | return true; |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | case Type::ArrayTyID: { |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 214 | const ArrayType *DAT = cast<ArrayType>(DstTy); |
| 215 | const ArrayType *SAT = cast<ArrayType>(SrcTy); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 216 | if (DAT->getNumElements() != SAT->getNumElements()) return true; |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 217 | return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(), |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 218 | Pointers); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 219 | } |
Chris Lattner | f6f4f7a | 2008-06-16 18:27:53 +0000 | [diff] [blame] | 220 | case Type::VectorTyID: { |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 221 | const VectorType *DVT = cast<VectorType>(DstTy); |
| 222 | const VectorType *SVT = cast<VectorType>(SrcTy); |
Chris Lattner | f6f4f7a | 2008-06-16 18:27:53 +0000 | [diff] [blame] | 223 | if (DVT->getNumElements() != SVT->getNumElements()) return true; |
| 224 | return RecursiveResolveTypesI(DVT->getElementType(), SVT->getElementType(), |
| 225 | Pointers); |
| 226 | } |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 227 | case Type::PointerTyID: { |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 228 | const PointerType *DstPT = cast<PointerType>(DstTy); |
| 229 | const PointerType *SrcPT = cast<PointerType>(SrcTy); |
Chris Lattner | 9ddf2c8 | 2008-06-16 19:55:40 +0000 | [diff] [blame] | 230 | |
| 231 | if (DstPT->getAddressSpace() != SrcPT->getAddressSpace()) |
| 232 | return true; |
| 233 | |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 234 | // If this is a pointer type, check to see if we have already seen it. If |
| 235 | // so, we are in a recursive branch. Cut off the search now. We cannot use |
| 236 | // an associative container for this search, because the type pointers (keys |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 237 | // in the container) change whenever types get resolved. |
| 238 | if (SrcPT->isAbstract()) |
| 239 | if (const Type *ExistingDestTy = Pointers.lookup(SrcPT)) |
| 240 | return ExistingDestTy != DstPT; |
| 241 | |
| 242 | if (DstPT->isAbstract()) |
| 243 | if (const Type *ExistingSrcTy = Pointers.lookup(DstPT)) |
| 244 | return ExistingSrcTy != SrcPT; |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 245 | // Otherwise, add the current pointers to the vector to stop recursion on |
| 246 | // this pair. |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 247 | if (DstPT->isAbstract()) |
| 248 | Pointers.insert(DstPT, SrcPT); |
| 249 | if (SrcPT->isAbstract()) |
| 250 | Pointers.insert(SrcPT, DstPT); |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 9ddf2c8 | 2008-06-16 19:55:40 +0000 | [diff] [blame] | 252 | return RecursiveResolveTypesI(DstPT->getElementType(), |
| 253 | SrcPT->getElementType(), Pointers); |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 254 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 255 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 258 | static bool RecursiveResolveTypes(const Type *DestTy, const Type *SrcTy) { |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 259 | LinkerTypeMap PointerTypes; |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 260 | return RecursiveResolveTypesI(DestTy, SrcTy, PointerTypes); |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 264 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 265 | // types are named in the src module that are not named in the Dst module. |
| 266 | // Make sure there are no type name conflicts. |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 267 | static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) { |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 268 | TypeSymbolTable *DestST = &Dest->getTypeSymbolTable(); |
| 269 | const TypeSymbolTable *SrcST = &Src->getTypeSymbolTable(); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 270 | |
| 271 | // Look for a type plane for Type's... |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 272 | TypeSymbolTable::const_iterator TI = SrcST->begin(); |
| 273 | TypeSymbolTable::const_iterator TE = SrcST->end(); |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 274 | if (TI == TE) return false; // No named types, do nothing. |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 275 | |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 276 | // Some types cannot be resolved immediately because they depend on other |
| 277 | // types being resolved to each other first. This contains a list of types we |
| 278 | // are waiting to recheck. |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 279 | std::vector<std::string> DelayedTypesToResolve; |
| 280 | |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 281 | for ( ; TI != TE; ++TI ) { |
| 282 | const std::string &Name = TI->first; |
Reid Spencer | c28a224 | 2004-07-04 11:52:49 +0000 | [diff] [blame] | 283 | const Type *RHS = TI->second; |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 284 | |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 285 | // Check to see if this type name is already in the dest module. |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 286 | Type *Entry = DestST->lookup(Name); |
Chris Lattner | 2f6bb2b | 2003-01-30 20:53:43 +0000 | [diff] [blame] | 287 | |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 288 | // If the name is just in the source module, bring it over to the dest. |
| 289 | if (Entry == 0) { |
| 290 | if (!Name.empty()) |
| 291 | DestST->insert(Name, const_cast<Type*>(RHS)); |
| 292 | } else if (ResolveTypes(Entry, RHS)) { |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 293 | // They look different, save the types 'till later to resolve. |
| 294 | DelayedTypesToResolve.push_back(Name); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 297 | |
| 298 | // Iteratively resolve types while we can... |
| 299 | while (!DelayedTypesToResolve.empty()) { |
| 300 | // Loop over all of the types, attempting to resolve them if possible... |
| 301 | unsigned OldSize = DelayedTypesToResolve.size(); |
| 302 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 303 | // Try direct resolution by name... |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 304 | for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) { |
| 305 | const std::string &Name = DelayedTypesToResolve[i]; |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 306 | Type *T1 = SrcST->lookup(Name); |
| 307 | Type *T2 = DestST->lookup(Name); |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 308 | if (!ResolveTypes(T2, T1)) { |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 309 | // We are making progress! |
| 310 | DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); |
| 311 | --i; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // Did we not eliminate any types? |
| 316 | if (DelayedTypesToResolve.size() == OldSize) { |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 317 | // Attempt to resolve subelements of types. This allows us to merge these |
| 318 | // two types: { int* } and { opaque* } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 319 | for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) { |
| 320 | const std::string &Name = DelayedTypesToResolve[i]; |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 321 | if (!RecursiveResolveTypes(SrcST->lookup(Name), DestST->lookup(Name))) { |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 322 | // We are making progress! |
| 323 | DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 324 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 325 | // Go back to the main loop, perhaps we can resolve directly by name |
| 326 | // now... |
| 327 | break; |
| 328 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 330 | |
| 331 | // If we STILL cannot resolve the types, then there is something wrong. |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 332 | if (DelayedTypesToResolve.size() == OldSize) { |
Chris Lattner | aeb18ce | 2003-10-21 22:46:38 +0000 | [diff] [blame] | 333 | // Remove the symbol name from the destination. |
| 334 | DelayedTypesToResolve.pop_back(); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 335 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 343 | static void PrintMap(const std::map<const Value*, Value*> &M) { |
| 344 | for (std::map<const Value*, Value*>::const_iterator I = M.begin(), E =M.end(); |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 345 | I != E; ++I) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 346 | cerr << " Fr: " << (void*)I->first << " "; |
Chris Lattner | 87182ae | 2002-04-07 22:31:23 +0000 | [diff] [blame] | 347 | I->first->dump(); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 348 | cerr << " To: " << (void*)I->second << " "; |
Chris Lattner | 87182ae | 2002-04-07 22:31:23 +0000 | [diff] [blame] | 349 | I->second->dump(); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 350 | cerr << "\n"; |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | |
Reid Spencer | 619f024 | 2007-02-04 04:43:17 +0000 | [diff] [blame] | 355 | // RemapOperand - Use ValueMap to convert constants from one module to another. |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 356 | static Value *RemapOperand(const Value *In, |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 357 | std::map<const Value*, Value*> &ValueMap) { |
| 358 | std::map<const Value*,Value*>::const_iterator I = ValueMap.find(In); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 359 | if (I != ValueMap.end()) |
| 360 | return I->second; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 361 | |
Reid Spencer | 619f024 | 2007-02-04 04:43:17 +0000 | [diff] [blame] | 362 | // Check to see if it's a constant that we are interested in transforming. |
Chris Lattner | 620fd68 | 2006-06-01 19:14:22 +0000 | [diff] [blame] | 363 | Value *Result = 0; |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 364 | if (const Constant *CPV = dyn_cast<Constant>(In)) { |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 365 | if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) || |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 366 | isa<ConstantInt>(CPV) || isa<ConstantAggregateZero>(CPV)) |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 367 | return const_cast<Constant*>(CPV); // Simple constants stay identical. |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 369 | if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) { |
Alkis Evlogimenos | cc7ba49 | 2004-08-04 08:08:13 +0000 | [diff] [blame] | 370 | std::vector<Constant*> Operands(CPA->getNumOperands()); |
| 371 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 372 | Operands[i] =cast<Constant>(RemapOperand(CPA->getOperand(i), ValueMap)); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 373 | Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 374 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) { |
Alkis Evlogimenos | cc7ba49 | 2004-08-04 08:08:13 +0000 | [diff] [blame] | 375 | std::vector<Constant*> Operands(CPS->getNumOperands()); |
| 376 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 377 | Operands[i] =cast<Constant>(RemapOperand(CPS->getOperand(i), ValueMap)); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 378 | Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands); |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 379 | } else if (isa<ConstantPointerNull>(CPV) || isa<UndefValue>(CPV)) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 380 | Result = const_cast<Constant*>(CPV); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 381 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CPV)) { |
Chris Lattner | a88eb92 | 2006-01-19 23:15:58 +0000 | [diff] [blame] | 382 | std::vector<Constant*> Operands(CP->getNumOperands()); |
| 383 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 384 | Operands[i] = cast<Constant>(RemapOperand(CP->getOperand(i), ValueMap)); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 385 | Result = ConstantVector::get(Operands); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 386 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
Chris Lattner | 27d6721 | 2006-07-14 22:21:31 +0000 | [diff] [blame] | 387 | std::vector<Constant*> Ops; |
| 388 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 389 | Ops.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),ValueMap))); |
| 390 | Result = CE->getWithOperands(Ops); |
Reid Spencer | 619f024 | 2007-02-04 04:43:17 +0000 | [diff] [blame] | 391 | } else if (isa<GlobalValue>(CPV)) { |
| 392 | assert(0 && "Unmapped global?"); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 393 | } else { |
| 394 | assert(0 && "Unknown type of derived type constant value!"); |
| 395 | } |
Chris Lattner | 620fd68 | 2006-06-01 19:14:22 +0000 | [diff] [blame] | 396 | } else if (isa<InlineAsm>(In)) { |
| 397 | Result = const_cast<Value*>(In); |
| 398 | } |
| 399 | |
Reid Spencer | 619f024 | 2007-02-04 04:43:17 +0000 | [diff] [blame] | 400 | // Cache the mapping in our local map structure |
Chris Lattner | 620fd68 | 2006-06-01 19:14:22 +0000 | [diff] [blame] | 401 | if (Result) { |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 402 | ValueMap[In] = Result; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 403 | return Result; |
| 404 | } |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 406 | |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 407 | cerr << "LinkModules ValueMap: \n"; |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 408 | PrintMap(ValueMap); |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 409 | |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 410 | cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n"; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 411 | assert(0 && "Couldn't remap value!"); |
| 412 | return 0; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 415 | /// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict |
| 416 | /// in the symbol table. This is good for all clients except for us. Go |
| 417 | /// through the trouble to force this back. |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 418 | static void ForceRenaming(GlobalValue *GV, const std::string &Name) { |
| 419 | assert(GV->getName() != Name && "Can't force rename to self"); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 420 | ValueSymbolTable &ST = GV->getParent()->getValueSymbolTable(); |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 421 | |
| 422 | // If there is a conflict, rename the conflict. |
Chris Lattner | 33f2949 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 423 | if (GlobalValue *ConflictGV = cast_or_null<GlobalValue>(ST.lookup(Name))) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 424 | assert(ConflictGV->hasInternalLinkage() && |
| 425 | "Not conflicting with a static global, should link instead!"); |
Chris Lattner | 33f2949 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 426 | GV->takeName(ConflictGV); |
| 427 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 428 | assert(ConflictGV->getName() != Name && "ForceRenaming didn't work"); |
Chris Lattner | 33f2949 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 429 | } else { |
| 430 | GV->setName(Name); // Force the name back |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 431 | } |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 432 | } |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 433 | |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 434 | /// CopyGVAttributes - copy additional attributes (those not needed to construct |
| 435 | /// a GlobalValue) from the SrcGV to the DestGV. |
| 436 | static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) { |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 437 | // Use the maximum alignment, rather than just copying the alignment of SrcGV. |
| 438 | unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment()); |
| 439 | DestGV->copyAttributesFrom(SrcGV); |
| 440 | DestGV->setAlignment(Alignment); |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 443 | /// GetLinkageResult - This analyzes the two global values and determines what |
| 444 | /// the result will look like in the destination module. In particular, it |
| 445 | /// computes the resultant linkage type, computes whether the global in the |
| 446 | /// source should be copied over to the destination (replacing the existing |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 447 | /// one), and computes whether this linkage is an error or not. It also performs |
| 448 | /// visibility checks: we cannot link together two symbols with different |
| 449 | /// visibilities. |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 450 | static bool GetLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 451 | GlobalValue::LinkageTypes <, bool &LinkFromSrc, |
| 452 | std::string *Err) { |
| 453 | assert((!Dest || !Src->hasInternalLinkage()) && |
| 454 | "If Src has internal linkage, Dest shouldn't be set!"); |
| 455 | if (!Dest) { |
| 456 | // Linking something to nothing. |
| 457 | LinkFromSrc = true; |
| 458 | LT = Src->getLinkage(); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 459 | } else if (Src->isDeclaration()) { |
Anton Korobeynikov | 2b48ef0 | 2008-03-10 22:33:22 +0000 | [diff] [blame] | 460 | // 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] | 461 | // external globals, we aren't adding anything. |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 462 | if (Src->hasDLLImportLinkage()) { |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 463 | // If one of GVs has DLLImport linkage, result should be dllimport'ed. |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 464 | if (Dest->isDeclaration()) { |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 465 | LinkFromSrc = true; |
| 466 | LT = Src->getLinkage(); |
| 467 | } |
Andrew Lenharth | 8753c44 | 2006-12-15 17:35:32 +0000 | [diff] [blame] | 468 | } else if (Dest->hasExternalWeakLinkage()) { |
| 469 | //If the Dest is weak, use the source linkage |
| 470 | LinkFromSrc = true; |
| 471 | LT = Src->getLinkage(); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 472 | } else { |
| 473 | LinkFromSrc = false; |
| 474 | LT = Dest->getLinkage(); |
| 475 | } |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 476 | } else if (Dest->isDeclaration() && !Dest->hasDLLImportLinkage()) { |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 477 | // If Dest is external but Src is not: |
| 478 | LinkFromSrc = true; |
| 479 | LT = Src->getLinkage(); |
| 480 | } else if (Src->hasAppendingLinkage() || Dest->hasAppendingLinkage()) { |
| 481 | if (Src->getLinkage() != Dest->getLinkage()) |
| 482 | return Error(Err, "Linking globals named '" + Src->getName() + |
| 483 | "': can only link appending global with another appending global!"); |
| 484 | LinkFromSrc = true; // Special cased. |
| 485 | LT = Src->getLinkage(); |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 486 | } else if (Src->isWeakForLinker()) { |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 487 | // At this point we know that Dest has LinkOnce, External*, Weak, Common, |
| 488 | // or DLL* linkage. |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 489 | if ((Dest->hasLinkOnceLinkage() && |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 490 | (Src->hasWeakLinkage() || Src->hasCommonLinkage())) || |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 491 | Dest->hasExternalWeakLinkage()) { |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 492 | LinkFromSrc = true; |
| 493 | LT = Src->getLinkage(); |
| 494 | } else { |
| 495 | LinkFromSrc = false; |
| 496 | LT = Dest->getLinkage(); |
| 497 | } |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 498 | } else if (Dest->isWeakForLinker()) { |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 499 | // At this point we know that Src has External* or DLL* linkage. |
| 500 | if (Src->hasExternalWeakLinkage()) { |
| 501 | LinkFromSrc = false; |
| 502 | LT = Dest->getLinkage(); |
| 503 | } else { |
| 504 | LinkFromSrc = true; |
| 505 | LT = GlobalValue::ExternalLinkage; |
| 506 | } |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 507 | } else { |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 508 | assert((Dest->hasExternalLinkage() || |
| 509 | Dest->hasDLLImportLinkage() || |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 510 | Dest->hasDLLExportLinkage() || |
| 511 | Dest->hasExternalWeakLinkage()) && |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 512 | (Src->hasExternalLinkage() || |
| 513 | Src->hasDLLImportLinkage() || |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 514 | Src->hasDLLExportLinkage() || |
| 515 | Src->hasExternalWeakLinkage()) && |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 516 | "Unexpected linkage type!"); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 517 | return Error(Err, "Linking globals named '" + Src->getName() + |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 518 | "': symbol multiply defined!"); |
| 519 | } |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 520 | |
| 521 | // Check visibility |
| 522 | if (Dest && Src->getVisibility() != Dest->getVisibility()) |
Chris Lattner | 97f8b09 | 2007-08-19 22:22:54 +0000 | [diff] [blame] | 523 | if (!Src->isDeclaration() && !Dest->isDeclaration()) |
| 524 | return Error(Err, "Linking globals named '" + Src->getName() + |
| 525 | "': symbols have different visibilities!"); |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 526 | return false; |
| 527 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 528 | |
| 529 | // LinkGlobals - Loop through the global variables in the src module and merge |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 530 | // them into the dest module. |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 531 | static bool LinkGlobals(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 532 | std::map<const Value*, Value*> &ValueMap, |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 533 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 534 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 535 | // Loop over all of the globals in the src module, mapping them over as we go |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 536 | for (Module::const_global_iterator I = Src->global_begin(), E = Src->global_end(); |
Chris Lattner | 1127315 | 2006-06-16 01:24:04 +0000 | [diff] [blame] | 537 | I != E; ++I) { |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 538 | const GlobalVariable *SGV = I; |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 539 | GlobalValue *DGV = 0; |
| 540 | |
| 541 | // Check to see if may have to link the global with the global |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 542 | if (SGV->hasName() && !SGV->hasInternalLinkage()) { |
| 543 | DGV = Dest->getGlobalVariable(SGV->getName()); |
| 544 | if (DGV && DGV->getType() != SGV->getType()) |
| 545 | // If types don't agree due to opaque types, try to resolve them. |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 546 | RecursiveResolveTypes(SGV->getType(), DGV->getType()); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 547 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 548 | |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 549 | // Check to see if may have to link the global with the alias |
Anton Korobeynikov | aeb0996 | 2008-03-10 22:35:31 +0000 | [diff] [blame] | 550 | if (!DGV && SGV->hasName() && !SGV->hasInternalLinkage()) { |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 551 | DGV = Dest->getNamedAlias(SGV->getName()); |
| 552 | if (DGV && DGV->getType() != SGV->getType()) |
| 553 | // If types don't agree due to opaque types, try to resolve them. |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 554 | RecursiveResolveTypes(SGV->getType(), DGV->getType()); |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 557 | if (DGV && DGV->hasInternalLinkage()) |
| 558 | DGV = 0; |
| 559 | |
Dan Gohman | c318329 | 2007-10-08 15:13:30 +0000 | [diff] [blame] | 560 | assert((SGV->hasInitializer() || SGV->hasExternalWeakLinkage() || |
| 561 | SGV->hasExternalLinkage() || SGV->hasDLLImportLinkage()) && |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 562 | "Global must either be external or have an initializer!"); |
| 563 | |
Chris Lattner | b324bd7 | 2006-11-09 05:18:12 +0000 | [diff] [blame] | 564 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 565 | bool LinkFromSrc = false; |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 566 | if (GetLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc, Err)) |
| 567 | return true; |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 568 | |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 569 | if (!DGV) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 570 | // No linking to be performed, simply create an identical version of the |
| 571 | // symbol over in the dest module... the initializer will be filled in |
| 572 | // later by LinkGlobalInits... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 573 | GlobalVariable *NewDGV = |
| 574 | new GlobalVariable(SGV->getType()->getElementType(), |
| 575 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
Chris Lattner | a534b0f | 2008-06-27 03:10:24 +0000 | [diff] [blame] | 576 | SGV->getName(), Dest, false, |
| 577 | SGV->getType()->getAddressSpace()); |
Reid Spencer | 471feac | 2007-02-04 04:30:33 +0000 | [diff] [blame] | 578 | // Propagate alignment, visibility and section info. |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 579 | CopyGVAttributes(NewDGV, SGV); |
Andrew Lenharth | 5dfbaf1 | 2007-02-01 17:12:54 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 581 | // If the LLVM runtime renamed the global, but it is an externally visible |
| 582 | // symbol, DGV must be an existing global with internal linkage. Rename |
| 583 | // it. |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 584 | if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()) |
| 585 | ForceRenaming(NewDGV, SGV->getName()); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 586 | |
| 587 | // Make sure to remember this mapping... |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 588 | ValueMap[SGV] = NewDGV; |
| 589 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 590 | if (SGV->hasAppendingLinkage()) |
| 591 | // Keep track that this is an appending variable... |
| 592 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 593 | } else if (DGV->hasAppendingLinkage()) { |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 594 | // No linking is performed yet. Just insert a new copy of the global, and |
| 595 | // keep track of the fact that it is an appending variable in the |
| 596 | // AppendingVars map. The name is cleared out so that no linkage is |
| 597 | // performed. |
| 598 | GlobalVariable *NewDGV = |
| 599 | new GlobalVariable(SGV->getType()->getElementType(), |
| 600 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
Chris Lattner | a534b0f | 2008-06-27 03:10:24 +0000 | [diff] [blame] | 601 | "", Dest, false, |
| 602 | SGV->getType()->getAddressSpace()); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 603 | |
Anton Korobeynikov | 75c7915 | 2008-03-07 18:34:50 +0000 | [diff] [blame] | 604 | // Set alignment allowing CopyGVAttributes merge it with alignment of SGV. |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 605 | NewDGV->setAlignment(DGV->getAlignment()); |
Anton Korobeynikov | 75c7915 | 2008-03-07 18:34:50 +0000 | [diff] [blame] | 606 | // Propagate alignment, section and visibility info. |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 607 | CopyGVAttributes(NewDGV, SGV); |
Andrew Lenharth | 5dfbaf1 | 2007-02-01 17:12:54 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 609 | // Make sure to remember this mapping... |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 610 | ValueMap[SGV] = NewDGV; |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 611 | |
| 612 | // Keep track that this is an appending variable... |
| 613 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 614 | } else if (GlobalAlias *DGA = dyn_cast<GlobalAlias>(DGV)) { |
| 615 | // SGV is global, but DGV is alias. The only valid mapping is when SGV is |
| 616 | // external declaration, which is effectively a no-op. Also make sure |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 617 | // linkage calculation was correct. |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 618 | if (SGV->isDeclaration() && !LinkFromSrc) { |
| 619 | // Make sure to remember this mapping... |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 620 | ValueMap[SGV] = DGA; |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 621 | } else |
Anton Korobeynikov | 1438b9d | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 622 | return Error(Err, "Global-Alias Collision on '" + SGV->getName() + |
| 623 | "': symbol multiple defined"); |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 624 | } else if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) { |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 625 | // Otherwise, perform the global-global mapping as instructed by |
| 626 | // GetLinkageResult. |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 627 | if (LinkFromSrc) { |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 628 | // Propagate alignment, section, and visibility info. |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 629 | CopyGVAttributes(DGVar, SGV); |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 630 | |
| 631 | // If the types don't match, and if we are to link from the source, nuke |
| 632 | // DGV and create a new one of the appropriate type. |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 633 | if (SGV->getType() != DGVar->getType()) { |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 634 | GlobalVariable *NewDGV = |
| 635 | new GlobalVariable(SGV->getType()->getElementType(), |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 636 | DGVar->isConstant(), DGVar->getLinkage(), |
Chris Lattner | a534b0f | 2008-06-27 03:10:24 +0000 | [diff] [blame] | 637 | /*init*/0, DGVar->getName(), Dest, false, |
| 638 | SGV->getType()->getAddressSpace()); |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 639 | CopyGVAttributes(NewDGV, DGVar); |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 640 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 641 | DGVar->getType())); |
| 642 | // DGVar will conflict with NewDGV because they both had the same |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 643 | // name. We must erase this now so ForceRenaming doesn't assert |
| 644 | // because DGV might not have internal linkage. |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 645 | DGVar->eraseFromParent(); |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 646 | |
| 647 | // If the symbol table renamed the global, but it is an externally |
| 648 | // visible symbol, DGV must be an existing global with internal |
| 649 | // linkage. Rename it. |
| 650 | if (NewDGV->getName() != SGV->getName() && |
| 651 | !NewDGV->hasInternalLinkage()) |
| 652 | ForceRenaming(NewDGV, SGV->getName()); |
| 653 | |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 654 | DGVar = NewDGV; |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 657 | // Inherit const as appropriate |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 658 | DGVar->setConstant(SGV->isConstant()); |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 659 | |
| 660 | // Set initializer to zero, so we can link the stuff later |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 661 | DGVar->setInitializer(0); |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 662 | } else { |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 663 | // Special case for const propagation |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 664 | if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant()) |
| 665 | DGVar->setConstant(true); |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 668 | // Set calculated linkage |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 669 | DGVar->setLinkage(NewLinkage); |
Anton Korobeynikov | 968e39a | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 670 | |
| 671 | // Make sure to remember this mapping... |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 672 | ValueMap[SGV] = ConstantExpr::getBitCast(DGVar, SGV->getType()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | return false; |
| 676 | } |
| 677 | |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 678 | static GlobalValue::LinkageTypes |
| 679 | CalculateAliasLinkage(const GlobalValue *SGV, const GlobalValue *DGV) { |
| 680 | if (SGV->hasExternalLinkage() || DGV->hasExternalLinkage()) |
| 681 | return GlobalValue::ExternalLinkage; |
| 682 | else if (SGV->hasWeakLinkage() || DGV->hasWeakLinkage()) |
| 683 | return GlobalValue::WeakLinkage; |
| 684 | else { |
| 685 | assert(SGV->hasInternalLinkage() && DGV->hasInternalLinkage() && |
| 686 | "Unexpected linkage type"); |
| 687 | return GlobalValue::InternalLinkage; |
| 688 | } |
| 689 | } |
| 690 | |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 691 | // LinkAlias - Loop through the alias in the src module and link them into the |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 692 | // dest module. We're assuming, that all functions/global variables were already |
| 693 | // linked in. |
Anton Korobeynikov | 4fb2873 | 2008-03-05 15:27:21 +0000 | [diff] [blame] | 694 | static bool LinkAlias(Module *Dest, const Module *Src, |
| 695 | std::map<const Value*, Value*> &ValueMap, |
| 696 | std::string *Err) { |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 697 | // Loop over all alias in the src module |
| 698 | for (Module::const_alias_iterator I = Src->alias_begin(), |
| 699 | E = Src->alias_end(); I != E; ++I) { |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 700 | const GlobalAlias *SGA = I; |
| 701 | const GlobalValue *SAliasee = SGA->getAliasedGlobal(); |
| 702 | GlobalAlias *NewGA = NULL; |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 703 | |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 704 | // Globals were already linked, thus we can just query ValueMap for variant |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 705 | // of SAliasee in Dest. |
Ted Kremenek | 58d5e05 | 2008-03-09 18:32:50 +0000 | [diff] [blame] | 706 | std::map<const Value*,Value*>::const_iterator VMI = ValueMap.find(SAliasee); |
| 707 | assert(VMI != ValueMap.end() && "Aliasee not linked"); |
| 708 | GlobalValue* DAliasee = cast<GlobalValue>(VMI->second); |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 709 | GlobalValue* DGV = NULL; |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 710 | |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 711 | // Try to find something 'similar' to SGA in destination module. |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 712 | if (!DGV && !SGA->hasInternalLinkage()) { |
| 713 | DGV = Dest->getNamedAlias(SGA->getName()); |
Anton Korobeynikov | 4fb2873 | 2008-03-05 15:27:21 +0000 | [diff] [blame] | 714 | |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 715 | // If types don't agree due to opaque types, try to resolve them. |
| 716 | if (DGV && DGV->getType() != SGA->getType()) |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 717 | if (RecursiveResolveTypes(SGA->getType(), DGV->getType())) |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 718 | return Error(Err, "Alias Collision on '" + SGA->getName()+ |
| 719 | "': aliases have different types"); |
| 720 | } |
| 721 | |
| 722 | if (!DGV && !SGA->hasInternalLinkage()) { |
| 723 | DGV = Dest->getGlobalVariable(SGA->getName()); |
| 724 | |
| 725 | // If types don't agree due to opaque types, try to resolve them. |
| 726 | if (DGV && DGV->getType() != SGA->getType()) |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 727 | if (RecursiveResolveTypes(SGA->getType(), DGV->getType())) |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 728 | return Error(Err, "Alias Collision on '" + SGA->getName()+ |
| 729 | "': aliases have different types"); |
| 730 | } |
| 731 | |
| 732 | if (!DGV && !SGA->hasInternalLinkage()) { |
| 733 | DGV = Dest->getFunction(SGA->getName()); |
| 734 | |
| 735 | // If types don't agree due to opaque types, try to resolve them. |
| 736 | if (DGV && DGV->getType() != SGA->getType()) |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 737 | if (RecursiveResolveTypes(SGA->getType(), DGV->getType())) |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 738 | return Error(Err, "Alias Collision on '" + SGA->getName()+ |
| 739 | "': aliases have different types"); |
| 740 | } |
| 741 | |
| 742 | // No linking to be performed on internal stuff. |
| 743 | if (DGV && DGV->hasInternalLinkage()) |
| 744 | DGV = NULL; |
| 745 | |
| 746 | if (GlobalAlias *DGA = dyn_cast_or_null<GlobalAlias>(DGV)) { |
| 747 | // Types are known to be the same, check whether aliasees equal. As |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 748 | // globals are already linked we just need query ValueMap to find the |
| 749 | // mapping. |
| 750 | if (DAliasee == DGA->getAliasedGlobal()) { |
| 751 | // This is just two copies of the same alias. Propagate linkage, if |
| 752 | // necessary. |
| 753 | DGA->setLinkage(CalculateAliasLinkage(SGA, DGA)); |
| 754 | |
| 755 | NewGA = DGA; |
| 756 | // Proceed to 'common' steps |
| 757 | } else |
Anton Korobeynikov | 1438b9d | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 758 | return Error(Err, "Alias Collision on '" + SGA->getName()+ |
| 759 | "': aliases have different aliasees"); |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 760 | } else if (GlobalVariable *DGVar = dyn_cast_or_null<GlobalVariable>(DGV)) { |
Anton Korobeynikov | f88bc65 | 2008-07-05 23:33:22 +0000 | [diff] [blame] | 761 | // The only allowed way is to link alias with external declaration or weak |
| 762 | // symbol.. |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 763 | if (DGVar->isDeclaration() || DGVar->isWeakForLinker()) { |
Anton Korobeynikov | ed61c0b | 2008-03-10 22:36:53 +0000 | [diff] [blame] | 764 | // But only if aliasee is global too... |
| 765 | if (!isa<GlobalVariable>(DAliasee)) |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 766 | return Error(Err, "Global-Alias Collision on '" + SGA->getName() + |
| 767 | "': aliasee is not global variable"); |
Anton Korobeynikov | ed61c0b | 2008-03-10 22:36:53 +0000 | [diff] [blame] | 768 | |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 769 | NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(), |
| 770 | SGA->getName(), DAliasee, Dest); |
| 771 | CopyGVAttributes(NewGA, SGA); |
| 772 | |
| 773 | // Any uses of DGV need to change to NewGA, with cast, if needed. |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 774 | if (SGA->getType() != DGVar->getType()) |
| 775 | DGVar->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA, |
| 776 | DGVar->getType())); |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 777 | else |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 778 | DGVar->replaceAllUsesWith(NewGA); |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 779 | |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 780 | // DGVar will conflict with NewGA because they both had the same |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 781 | // name. We must erase this now so ForceRenaming doesn't assert |
| 782 | // because DGV might not have internal linkage. |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 783 | DGVar->eraseFromParent(); |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 784 | |
| 785 | // Proceed to 'common' steps |
| 786 | } else |
Anton Korobeynikov | 1438b9d | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 787 | return Error(Err, "Global-Alias Collision on '" + SGA->getName() + |
| 788 | "': symbol multiple defined"); |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 789 | } else if (Function *DF = dyn_cast_or_null<Function>(DGV)) { |
Anton Korobeynikov | f88bc65 | 2008-07-05 23:33:22 +0000 | [diff] [blame] | 790 | // The only allowed way is to link alias with external declaration or weak |
| 791 | // symbol... |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 792 | if (DF->isDeclaration() || DF->isWeakForLinker()) { |
Anton Korobeynikov | ed61c0b | 2008-03-10 22:36:53 +0000 | [diff] [blame] | 793 | // But only if aliasee is function too... |
| 794 | if (!isa<Function>(DAliasee)) |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 795 | return Error(Err, "Function-Alias Collision on '" + SGA->getName() + |
| 796 | "': aliasee is not function"); |
Anton Korobeynikov | ed61c0b | 2008-03-10 22:36:53 +0000 | [diff] [blame] | 797 | |
Anton Korobeynikov | b5a4bd8 | 2008-03-05 23:08:16 +0000 | [diff] [blame] | 798 | NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(), |
| 799 | SGA->getName(), DAliasee, Dest); |
| 800 | CopyGVAttributes(NewGA, SGA); |
| 801 | |
| 802 | // Any uses of DF need to change to NewGA, with cast, if needed. |
| 803 | if (SGA->getType() != DF->getType()) |
| 804 | DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA, |
| 805 | DF->getType())); |
| 806 | else |
| 807 | DF->replaceAllUsesWith(NewGA); |
| 808 | |
| 809 | // DF will conflict with NewGA because they both had the same |
| 810 | // name. We must erase this now so ForceRenaming doesn't assert |
| 811 | // because DF might not have internal linkage. |
| 812 | DF->eraseFromParent(); |
| 813 | |
| 814 | // Proceed to 'common' steps |
| 815 | } else |
Anton Korobeynikov | 1438b9d | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 816 | return Error(Err, "Function-Alias Collision on '" + SGA->getName() + |
| 817 | "': symbol multiple defined"); |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 818 | } else { |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 819 | // No linking to be performed, simply create an identical version of the |
| 820 | // alias over in the dest module... |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 821 | |
| 822 | NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(), |
| 823 | SGA->getName(), DAliasee, Dest); |
| 824 | CopyGVAttributes(NewGA, SGA); |
| 825 | |
| 826 | // Proceed to 'common' steps |
| 827 | } |
| 828 | |
| 829 | assert(NewGA && "No alias was created in destination module!"); |
| 830 | |
Anton Korobeynikov | b8cdaf7 | 2008-03-10 22:36:35 +0000 | [diff] [blame] | 831 | // If the symbol table renamed the alias, but it is an externally visible |
Anton Korobeynikov | caa8ae8 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 832 | // symbol, DGA must be an global value with internal linkage. Rename it. |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 833 | if (NewGA->getName() != SGA->getName() && |
| 834 | !NewGA->hasInternalLinkage()) |
| 835 | ForceRenaming(NewGA, SGA->getName()); |
| 836 | |
| 837 | // Remember this mapping so uses in the source module get remapped |
| 838 | // later by RemapOperand. |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 839 | ValueMap[SGA] = NewGA; |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 840 | } |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 841 | |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 842 | return false; |
| 843 | } |
| 844 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 846 | // LinkGlobalInits - Update the initializers in the Dest module now that all |
| 847 | // globals that may be referenced are in Dest. |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 848 | static bool LinkGlobalInits(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 849 | std::map<const Value*, Value*> &ValueMap, |
| 850 | std::string *Err) { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 851 | |
| 852 | // Loop over all of the globals in the src module, mapping them over as we go |
Chris Lattner | 1127315 | 2006-06-16 01:24:04 +0000 | [diff] [blame] | 853 | for (Module::const_global_iterator I = Src->global_begin(), |
| 854 | E = Src->global_end(); I != E; ++I) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 855 | const GlobalVariable *SGV = I; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 856 | |
| 857 | if (SGV->hasInitializer()) { // Only process initialized GV's |
| 858 | // Figure out what the initializer looks like in the dest module... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 859 | Constant *SInit = |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 860 | cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 861 | |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 862 | GlobalVariable *DGV = |
| 863 | cast<GlobalVariable>(ValueMap[SGV]->stripPointerCasts()); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 864 | if (DGV->hasInitializer()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 865 | if (SGV->hasExternalLinkage()) { |
| 866 | if (DGV->getInitializer() != SInit) |
Anton Korobeynikov | 1438b9d | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 867 | return Error(Err, "Global Variable Collision on '" + SGV->getName() + |
| 868 | "': global variables have different initializers"); |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 869 | } else if (DGV->isWeakForLinker()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 870 | // Nothing is required, mapped values will take the new global |
| 871 | // automatically. |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 872 | } else if (SGV->isWeakForLinker()) { |
Chris Lattner | 57cb988 | 2004-02-17 21:56:04 +0000 | [diff] [blame] | 873 | // Nothing is required, mapped values will take the new global |
| 874 | // automatically. |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 875 | } else if (DGV->hasAppendingLinkage()) { |
| 876 | assert(0 && "Appending linkage unimplemented!"); |
| 877 | } else { |
| 878 | assert(0 && "Unknown linkage!"); |
| 879 | } |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 880 | } else { |
| 881 | // Copy the initializer over now... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 882 | DGV->setInitializer(SInit); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | } |
| 886 | return false; |
| 887 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 888 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 889 | // LinkFunctionProtos - Link the functions together between the two modules, |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 890 | // without doing function bodies... this just adds external function prototypes |
| 891 | // to the Dest function... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 892 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 893 | static bool LinkFunctionProtos(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 894 | std::map<const Value*, Value*> &ValueMap, |
| 895 | std::string *Err) { |
Reid Spencer | 619f024 | 2007-02-04 04:43:17 +0000 | [diff] [blame] | 896 | // Loop over all of the functions in the src module, mapping them over |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 897 | for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 898 | const Function *SF = I; // SrcFunction |
Chris Lattner | 8246849 | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 899 | |
Anton Korobeynikov | 194c2ce | 2008-07-05 23:03:21 +0000 | [diff] [blame] | 900 | GlobalValue *DGV = 0; |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 901 | Value *MappedDF; |
Chris Lattner | 8246849 | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 902 | |
| 903 | // If this function is internal or has no name, it doesn't participate in |
| 904 | // linkage. |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 905 | if (SF->hasName() && !SF->hasInternalLinkage()) { |
| 906 | // Check to see if may have to link the function. |
Anton Korobeynikov | 194c2ce | 2008-07-05 23:03:21 +0000 | [diff] [blame] | 907 | DGV = Dest->getFunction(SF->getName()); |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 908 | } |
Anton Korobeynikov | 194c2ce | 2008-07-05 23:03:21 +0000 | [diff] [blame] | 909 | |
| 910 | // Check to see if may have to link the function with the alias |
| 911 | if (!DGV && SF->hasName() && !SF->hasInternalLinkage()) { |
| 912 | DGV = Dest->getNamedAlias(SF->getName()); |
| 913 | if (DGV && DGV->getType() != SF->getType()) |
| 914 | // If types don't agree due to opaque types, try to resolve them. |
| 915 | RecursiveResolveTypes(SF->getType(), DGV->getType()); |
| 916 | } |
| 917 | |
| 918 | if (DGV && DGV->hasInternalLinkage()) |
| 919 | DGV = 0; |
| 920 | |
Chris Lattner | 8246849 | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 921 | // If there is no linkage to be performed, just bring over SF without |
| 922 | // modifying it. |
Anton Korobeynikov | 194c2ce | 2008-07-05 23:03:21 +0000 | [diff] [blame] | 923 | if (DGV == 0) { |
Chris Lattner | 8246849 | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 924 | // Function does not already exist, simply insert an function signature |
| 925 | // identical to SF into the dest module. |
| 926 | Function *NewDF = Function::Create(SF->getFunctionType(), |
| 927 | SF->getLinkage(), |
| 928 | SF->getName(), Dest); |
| 929 | CopyGVAttributes(NewDF, SF); |
| 930 | |
| 931 | // If the LLVM runtime renamed the function, but it is an externally |
| 932 | // visible symbol, DF must be an existing function with internal linkage. |
| 933 | // Rename it. |
| 934 | if (!NewDF->hasInternalLinkage() && NewDF->getName() != SF->getName()) |
| 935 | ForceRenaming(NewDF, SF->getName()); |
| 936 | |
| 937 | // ... and remember this mapping... |
| 938 | ValueMap[SF] = NewDF; |
| 939 | continue; |
Anton Korobeynikov | 194c2ce | 2008-07-05 23:03:21 +0000 | [diff] [blame] | 940 | } else if (GlobalAlias *DGA = dyn_cast<GlobalAlias>(DGV)) { |
Anton Korobeynikov | f88bc65 | 2008-07-05 23:33:22 +0000 | [diff] [blame] | 941 | // SF is function, but DF is alias. |
| 942 | // The only valid mappings are: |
| 943 | // - SF is external declaration, which is effectively a no-op. |
| 944 | // - SF is weak, when we just need to throw SF out. |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 945 | if (!SF->isDeclaration() && !SF->isWeakForLinker()) |
Anton Korobeynikov | 194c2ce | 2008-07-05 23:03:21 +0000 | [diff] [blame] | 946 | return Error(Err, "Function-Alias Collision on '" + SF->getName() + |
| 947 | "': symbol multiple defined"); |
| 948 | |
| 949 | // Make sure to remember this mapping... |
Anton Korobeynikov | f88bc65 | 2008-07-05 23:33:22 +0000 | [diff] [blame] | 950 | ValueMap[SF] = ConstantExpr::getBitCast(DGA, SF->getType()); |
Anton Korobeynikov | 194c2ce | 2008-07-05 23:03:21 +0000 | [diff] [blame] | 951 | continue; |
Chris Lattner | 8246849 | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 952 | } |
Anton Korobeynikov | 194c2ce | 2008-07-05 23:03:21 +0000 | [diff] [blame] | 953 | |
| 954 | Function* DF = cast<Function>(DGV); |
Chris Lattner | 8246849 | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 955 | // If types don't agree because of opaque, try to resolve them. |
| 956 | if (SF->getType() != DF->getType()) |
Chris Lattner | bc1c82a | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 957 | RecursiveResolveTypes(SF->getType(), DF->getType()); |
Chris Lattner | 8246849 | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 958 | |
| 959 | // Check visibility, merging if a definition overrides a prototype. |
| 960 | if (SF->getVisibility() != DF->getVisibility()) { |
Chris Lattner | 97f8b09 | 2007-08-19 22:22:54 +0000 | [diff] [blame] | 961 | // If one is a prototype, ignore its visibility. Prototypes are always |
| 962 | // overridden by the definition. |
| 963 | if (!SF->isDeclaration() && !DF->isDeclaration()) |
| 964 | return Error(Err, "Linking functions named '" + SF->getName() + |
| 965 | "': symbols have different visibilities!"); |
Chris Lattner | bc3d1c7 | 2008-06-09 07:25:28 +0000 | [diff] [blame] | 966 | |
| 967 | // Otherwise, replace the visibility of DF if DF is a prototype. |
| 968 | if (DF->isDeclaration()) |
| 969 | DF->setVisibility(SF->getVisibility()); |
Chris Lattner | 97f8b09 | 2007-08-19 22:22:54 +0000 | [diff] [blame] | 970 | } |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 971 | |
Chris Lattner | 8246849 | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 972 | if (DF->getType() != SF->getType()) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 973 | if (DF->isDeclaration() && !SF->isDeclaration()) { |
| 974 | // We have a definition of the same name but different type in the |
| 975 | // source module. Copy the prototype to the destination and replace |
| 976 | // uses of the destination's prototype with the new prototype. |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 977 | Function *NewDF = Function::Create(SF->getFunctionType(), |
| 978 | SF->getLinkage(), |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 979 | SF->getName(), Dest); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 980 | CopyGVAttributes(NewDF, SF); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 981 | |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 982 | // Any uses of DF need to change to NewDF, with cast |
| 983 | DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DF->getType())); |
| 984 | |
| 985 | // DF will conflict with NewDF because they both had the same. We must |
| 986 | // erase this now so ForceRenaming doesn't assert because DF might |
| 987 | // not have internal linkage. |
| 988 | DF->eraseFromParent(); |
| 989 | |
| 990 | // If the symbol table renamed the function, but it is an externally |
| 991 | // visible symbol, DF must be an existing function with internal |
| 992 | // linkage. Rename it. |
| 993 | if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) |
| 994 | ForceRenaming(NewDF, SF->getName()); |
| 995 | |
| 996 | // Remember this mapping so uses in the source module get remapped |
| 997 | // later by RemapOperand. |
| 998 | ValueMap[SF] = NewDF; |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 999 | continue; |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1000 | } else { |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 1001 | // We have two functions of the same name but different type. Any use |
| 1002 | // of the source must be mapped to the destination, with a cast. |
| 1003 | MappedDF = ConstantExpr::getBitCast(DF, SF->getType()); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1004 | } |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 1005 | } else { |
| 1006 | MappedDF = DF; |
Chris Lattner | 8246849 | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | if (SF->isDeclaration()) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1010 | // If SF is a declaration or if both SF & DF are declarations, just link |
| 1011 | // the declarations, we aren't adding anything. |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 1012 | if (SF->hasDLLImportLinkage()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1013 | if (DF->isDeclaration()) { |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 1014 | ValueMap[SF] = MappedDF; |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 1015 | DF->setLinkage(SF->getLinkage()); |
Chris Lattner | 822143e | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 1016 | } |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 1017 | } else { |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 1018 | ValueMap[SF] = MappedDF; |
Chris Lattner | 822143e | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 1019 | } |
| 1020 | continue; |
| 1021 | } |
| 1022 | |
| 1023 | // If DF is external but SF is not, link the external functions, update |
| 1024 | // linkage qualifiers. |
| 1025 | if (DF->isDeclaration() && !DF->hasDLLImportLinkage()) { |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 1026 | ValueMap.insert(std::make_pair(SF, MappedDF)); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 1027 | DF->setLinkage(SF->getLinkage()); |
Chris Lattner | 822143e | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 1028 | continue; |
| 1029 | } |
| 1030 | |
| 1031 | // At this point we know that DF has LinkOnce, Weak, or External* linkage. |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 1032 | if (SF->isWeakForLinker()) { |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 1033 | ValueMap[SF] = MappedDF; |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 1035 | // Linkonce+Weak = Weak |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 1036 | // *+External Weak = * |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 1037 | if ((DF->hasLinkOnceLinkage() && |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 1038 | (SF->hasWeakLinkage() || SF->hasCommonLinkage())) || |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 1039 | DF->hasExternalWeakLinkage()) |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 1040 | DF->setLinkage(SF->getLinkage()); |
Chris Lattner | 822143e | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 1041 | continue; |
| 1042 | } |
| 1043 | |
Anton Korobeynikov | 80585f1 | 2008-07-05 23:48:30 +0000 | [diff] [blame^] | 1044 | if (DF->isWeakForLinker()) { |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 1045 | // At this point we know that SF has LinkOnce or External* linkage. |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 1046 | ValueMap[SF] = MappedDF; |
Chris Lattner | 822143e | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 1047 | |
| 1048 | // If the source function has stronger linkage than the destination, |
| 1049 | // its body and linkage should override ours. |
| 1050 | if (!SF->hasLinkOnceLinkage() && !SF->hasExternalWeakLinkage()) { |
| 1051 | // Don't inherit linkonce & external weak linkage. |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 1052 | DF->setLinkage(SF->getLinkage()); |
Chris Lattner | 822143e | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 1053 | DF->deleteBody(); |
| 1054 | } |
| 1055 | continue; |
| 1056 | } |
| 1057 | |
| 1058 | if (SF->getLinkage() != DF->getLinkage()) |
| 1059 | return Error(Err, "Functions named '" + SF->getName() + |
| 1060 | "' have different linkage specifiers!"); |
| 1061 | |
| 1062 | // The function is defined identically in both modules! |
| 1063 | if (SF->hasExternalLinkage()) |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 1064 | return Error(Err, "Function '" + |
| 1065 | ToStr(SF->getFunctionType(), Src) + "':\"" + |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 1066 | SF->getName() + "\" - Function is already defined!"); |
Chris Lattner | 822143e | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 1067 | assert(0 && "Unknown linkage configuration found!"); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1068 | } |
| 1069 | return false; |
| 1070 | } |
| 1071 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 1072 | // LinkFunctionBody - Copy the source function over into the dest function and |
| 1073 | // fix up references to values. At this point we know that Dest is an external |
| 1074 | // function, and that Src is not. |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 1075 | static bool LinkFunctionBody(Function *Dest, Function *Src, |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1076 | std::map<const Value*, Value*> &ValueMap, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 1077 | std::string *Err) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1078 | assert(Src && Dest && Dest->isDeclaration() && !Src->isDeclaration()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 1080 | // Go through and convert function arguments over, remembering the mapping. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1081 | Function::arg_iterator DI = Dest->arg_begin(); |
| 1082 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 1083 | I != E; ++I, ++DI) { |
Owen Anderson | 6bc41e8 | 2008-04-14 17:38:21 +0000 | [diff] [blame] | 1084 | DI->setName(I->getName()); // Copy the name information over... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1085 | |
| 1086 | // Add a mapping to our local map |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 1087 | ValueMap[I] = DI; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 1090 | // Splice the body of the source function into the dest function. |
| 1091 | Dest->getBasicBlockList().splice(Dest->end(), Src->getBasicBlockList()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 1093 | // At this point, all of the instructions and values of the function are now |
| 1094 | // copied over. The only problem is that they are still referencing values in |
| 1095 | // the Source function as operands. Loop through all of the operands of the |
| 1096 | // functions and patch them up to point to the local versions... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1097 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 1098 | for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB) |
| 1099 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 1100 | for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 1101 | OI != OE; ++OI) |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 1102 | if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI)) |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1103 | *OI = RemapOperand(*OI, ValueMap); |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 1104 | |
| 1105 | // There is no need to map the arguments anymore. |
Chris Lattner | 1127315 | 2006-06-16 01:24:04 +0000 | [diff] [blame] | 1106 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
| 1107 | I != E; ++I) |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1108 | ValueMap.erase(I); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1109 | |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
| 1113 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 1114 | // LinkFunctionBodies - Link in the function bodies that are defined in the |
| 1115 | // source module into the DestModule. This consists basically of copying the |
| 1116 | // function over and fixing up references to values. |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 1117 | static bool LinkFunctionBodies(Module *Dest, Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 1118 | std::map<const Value*, Value*> &ValueMap, |
| 1119 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1120 | |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 1121 | // Loop over all of the functions in the src module, mapping them over as we |
| 1122 | // go |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 1123 | for (Module::iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF) { |
Reid Spencer | 619f024 | 2007-02-04 04:43:17 +0000 | [diff] [blame] | 1124 | if (!SF->isDeclaration()) { // No body if function is external |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 1125 | Function *DF = dyn_cast<Function>(ValueMap[SF]); // Destination function |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1126 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 1127 | // DF not external SF external? |
Chris Lattner | ec91ccb | 2008-06-20 05:29:39 +0000 | [diff] [blame] | 1128 | if (DF && DF->isDeclaration()) |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 1129 | // Only provide the function body if there isn't one already. |
| 1130 | if (LinkFunctionBody(DF, SF, ValueMap, Err)) |
| 1131 | return true; |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 1132 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1133 | } |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1137 | // LinkAppendingVars - If there were any appending global variables, link them |
| 1138 | // together now. Return true on error. |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1139 | static bool LinkAppendingVars(Module *M, |
| 1140 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
| 1141 | std::string *ErrorMsg) { |
| 1142 | if (AppendingVars.empty()) return false; // Nothing to do. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1144 | // Loop over the multimap of appending vars, processing any variables with the |
| 1145 | // same name, forming a new appending global variable with both of the |
| 1146 | // initializers merged together, then rewrite references to the old variables |
| 1147 | // and delete them. |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1148 | std::vector<Constant*> Inits; |
| 1149 | while (AppendingVars.size() > 1) { |
| 1150 | // Get the first two elements in the map... |
| 1151 | std::multimap<std::string, |
| 1152 | GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++; |
| 1153 | |
| 1154 | // If the first two elements are for different names, there is no pair... |
| 1155 | // Otherwise there is a pair, so link them together... |
| 1156 | if (First->first == Second->first) { |
| 1157 | GlobalVariable *G1 = First->second, *G2 = Second->second; |
| 1158 | const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType()); |
| 1159 | const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType()); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 1160 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1161 | // Check to see that they two arrays agree on type... |
| 1162 | if (T1->getElementType() != T2->getElementType()) |
| 1163 | return Error(ErrorMsg, |
| 1164 | "Appending variables with different element types need to be linked!"); |
| 1165 | if (G1->isConstant() != G2->isConstant()) |
| 1166 | return Error(ErrorMsg, |
| 1167 | "Appending variables linked with different const'ness!"); |
| 1168 | |
Lauro Ramos Venancio | 9613e87 | 2007-06-06 22:01:12 +0000 | [diff] [blame] | 1169 | if (G1->getAlignment() != G2->getAlignment()) |
| 1170 | return Error(ErrorMsg, |
| 1171 | "Appending variables with different alignment need to be linked!"); |
| 1172 | |
| 1173 | if (G1->getVisibility() != G2->getVisibility()) |
| 1174 | return Error(ErrorMsg, |
| 1175 | "Appending variables with different visibility need to be linked!"); |
| 1176 | |
| 1177 | if (G1->getSection() != G2->getSection()) |
| 1178 | return Error(ErrorMsg, |
| 1179 | "Appending variables with different section name need to be linked!"); |
| 1180 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1181 | unsigned NewSize = T1->getNumElements() + T2->getNumElements(); |
| 1182 | ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize); |
| 1183 | |
Chris Lattner | ed74a4e | 2005-12-06 17:30:58 +0000 | [diff] [blame] | 1184 | G1->setName(""); // Clear G1's name in case of a conflict! |
| 1185 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1186 | // Create the new global variable... |
| 1187 | GlobalVariable *NG = |
| 1188 | new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(), |
Chris Lattner | a534b0f | 2008-06-27 03:10:24 +0000 | [diff] [blame] | 1189 | /*init*/0, First->first, M, G1->isThreadLocal(), |
| 1190 | G1->getType()->getAddressSpace()); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1191 | |
Lauro Ramos Venancio | 9613e87 | 2007-06-06 22:01:12 +0000 | [diff] [blame] | 1192 | // Propagate alignment, visibility and section info. |
| 1193 | CopyGVAttributes(NG, G1); |
| 1194 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1195 | // Merge the initializer... |
| 1196 | Inits.reserve(NewSize); |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 1197 | if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) { |
| 1198 | for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) |
Alkis Evlogimenos | cc7ba49 | 2004-08-04 08:08:13 +0000 | [diff] [blame] | 1199 | Inits.push_back(I->getOperand(i)); |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 1200 | } else { |
| 1201 | assert(isa<ConstantAggregateZero>(G1->getInitializer())); |
| 1202 | Constant *CV = Constant::getNullValue(T1->getElementType()); |
| 1203 | for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) |
| 1204 | Inits.push_back(CV); |
| 1205 | } |
| 1206 | if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) { |
| 1207 | for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) |
Alkis Evlogimenos | cc7ba49 | 2004-08-04 08:08:13 +0000 | [diff] [blame] | 1208 | Inits.push_back(I->getOperand(i)); |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 1209 | } else { |
| 1210 | assert(isa<ConstantAggregateZero>(G2->getInitializer())); |
| 1211 | Constant *CV = Constant::getNullValue(T2->getElementType()); |
| 1212 | for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) |
| 1213 | Inits.push_back(CV); |
| 1214 | } |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1215 | NG->setInitializer(ConstantArray::get(NewType, Inits)); |
| 1216 | Inits.clear(); |
| 1217 | |
| 1218 | // Replace any uses of the two global variables with uses of the new |
| 1219 | // global... |
| 1220 | |
| 1221 | // FIXME: This should rewrite simple/straight-forward uses such as |
| 1222 | // getelementptr instructions to not use the Cast! |
Reid Spencer | 4da4912 | 2006-12-12 05:05:00 +0000 | [diff] [blame] | 1223 | G1->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G1->getType())); |
| 1224 | G2->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G2->getType())); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1225 | |
| 1226 | // Remove the two globals from the module now... |
| 1227 | M->getGlobalList().erase(G1); |
| 1228 | M->getGlobalList().erase(G2); |
| 1229 | |
| 1230 | // Put the new global into the AppendingVars map so that we can handle |
| 1231 | // linking of more than two vars... |
| 1232 | Second->second = NG; |
| 1233 | } |
| 1234 | AppendingVars.erase(First); |
| 1235 | } |
| 1236 | |
| 1237 | return false; |
| 1238 | } |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1239 | |
Anton Korobeynikov | 9f2ee70 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1240 | static bool ResolveAliases(Module *Dest) { |
| 1241 | for (Module::alias_iterator I = Dest->alias_begin(), E = Dest->alias_end(); |
Anton Korobeynikov | 5241957 | 2008-03-11 22:51:09 +0000 | [diff] [blame] | 1242 | I != E; ++I) |
| 1243 | if (const GlobalValue *GV = I->resolveAliasedGlobal()) |
| 1244 | if (!GV->isDeclaration()) |
| 1245 | I->replaceAllUsesWith(const_cast<GlobalValue*>(GV)); |
Anton Korobeynikov | 9f2ee70 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1246 | |
| 1247 | return false; |
| 1248 | } |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1249 | |
| 1250 | // LinkModules - This function links two modules together, with the resulting |
| 1251 | // left module modified to be the composite of the two input modules. If an |
| 1252 | // 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] | 1253 | // the problem. Upon failure, the Dest module could be in a modified state, and |
| 1254 | // shouldn't be relied on to be consistent. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 1255 | bool |
Reid Spencer | 0ba9e21 | 2004-12-13 03:00:16 +0000 | [diff] [blame] | 1256 | Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 1257 | assert(Dest != 0 && "Invalid Destination module"); |
| 1258 | assert(Src != 0 && "Invalid Source Module"); |
| 1259 | |
Chris Lattner | c36357c | 2007-01-29 00:21:34 +0000 | [diff] [blame] | 1260 | if (Dest->getDataLayout().empty()) { |
| 1261 | if (!Src->getDataLayout().empty()) { |
Chris Lattner | ec9bfdc | 2007-01-29 02:18:13 +0000 | [diff] [blame] | 1262 | Dest->setDataLayout(Src->getDataLayout()); |
Chris Lattner | c36357c | 2007-01-29 00:21:34 +0000 | [diff] [blame] | 1263 | } else { |
| 1264 | std::string DataLayout; |
Reid Spencer | 26f2385 | 2007-01-26 08:11:39 +0000 | [diff] [blame] | 1265 | |
Anton Korobeynikov | a27694d | 2008-02-20 11:27:04 +0000 | [diff] [blame] | 1266 | if (Dest->getEndianness() == Module::AnyEndianness) { |
Chris Lattner | c36357c | 2007-01-29 00:21:34 +0000 | [diff] [blame] | 1267 | if (Src->getEndianness() == Module::BigEndian) |
| 1268 | DataLayout.append("E"); |
| 1269 | else if (Src->getEndianness() == Module::LittleEndian) |
| 1270 | DataLayout.append("e"); |
Anton Korobeynikov | a27694d | 2008-02-20 11:27:04 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | if (Dest->getPointerSize() == Module::AnyPointerSize) { |
Chris Lattner | c36357c | 2007-01-29 00:21:34 +0000 | [diff] [blame] | 1274 | if (Src->getPointerSize() == Module::Pointer64) |
| 1275 | DataLayout.append(DataLayout.length() == 0 ? "p:64:64" : "-p:64:64"); |
| 1276 | else if (Src->getPointerSize() == Module::Pointer32) |
| 1277 | DataLayout.append(DataLayout.length() == 0 ? "p:32:32" : "-p:32:32"); |
Anton Korobeynikov | a27694d | 2008-02-20 11:27:04 +0000 | [diff] [blame] | 1278 | } |
Chris Lattner | c36357c | 2007-01-29 00:21:34 +0000 | [diff] [blame] | 1279 | Dest->setDataLayout(DataLayout); |
| 1280 | } |
| 1281 | } |
| 1282 | |
Chris Lattner | f27dfcb | 2008-02-19 18:49:08 +0000 | [diff] [blame] | 1283 | // Copy the target triple from the source to dest if the dest's is empty. |
Chris Lattner | c36357c | 2007-01-29 00:21:34 +0000 | [diff] [blame] | 1284 | if (Dest->getTargetTriple().empty() && !Src->getTargetTriple().empty()) |
Chris Lattner | 152f19a | 2004-12-10 20:26:15 +0000 | [diff] [blame] | 1285 | Dest->setTargetTriple(Src->getTargetTriple()); |
Chris Lattner | c36357c | 2007-01-29 00:21:34 +0000 | [diff] [blame] | 1286 | |
| 1287 | if (!Src->getDataLayout().empty() && !Dest->getDataLayout().empty() && |
| 1288 | Src->getDataLayout() != Dest->getDataLayout()) |
Reid Spencer | 26f2385 | 2007-01-26 08:11:39 +0000 | [diff] [blame] | 1289 | cerr << "WARNING: Linking two modules of different data layouts!\n"; |
Chris Lattner | 152f19a | 2004-12-10 20:26:15 +0000 | [diff] [blame] | 1290 | if (!Src->getTargetTriple().empty() && |
| 1291 | Dest->getTargetTriple() != Src->getTargetTriple()) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1292 | cerr << "WARNING: Linking two modules of different target triples!\n"; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 1293 | |
Chris Lattner | f27dfcb | 2008-02-19 18:49:08 +0000 | [diff] [blame] | 1294 | // Append the module inline asm string. |
Chris Lattner | 6631601 | 2006-01-24 04:14:29 +0000 | [diff] [blame] | 1295 | if (!Src->getModuleInlineAsm().empty()) { |
| 1296 | if (Dest->getModuleInlineAsm().empty()) |
| 1297 | Dest->setModuleInlineAsm(Src->getModuleInlineAsm()); |
Chris Lattner | e1b2e14 | 2006-01-23 23:08:37 +0000 | [diff] [blame] | 1298 | else |
Chris Lattner | 6631601 | 2006-01-24 04:14:29 +0000 | [diff] [blame] | 1299 | Dest->setModuleInlineAsm(Dest->getModuleInlineAsm()+"\n"+ |
| 1300 | Src->getModuleInlineAsm()); |
Chris Lattner | e1b2e14 | 2006-01-23 23:08:37 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Reid Spencer | 719012d | 2004-11-25 09:29:44 +0000 | [diff] [blame] | 1303 | // Update the destination module's dependent libraries list with the libraries |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 1304 | // from the source module. There's no opportunity for duplicates here as the |
| 1305 | // Module ensures that duplicate insertions are discarded. |
Chris Lattner | f27dfcb | 2008-02-19 18:49:08 +0000 | [diff] [blame] | 1306 | for (Module::lib_iterator SI = Src->lib_begin(), SE = Src->lib_end(); |
| 1307 | SI != SE; ++SI) |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 1308 | Dest->addLibrary(*SI); |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 1309 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 1310 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 1311 | // types are named in the src module that are not named in the Dst module. |
| 1312 | // Make sure there are no type name conflicts. |
Reid Spencer | 619f024 | 2007-02-04 04:43:17 +0000 | [diff] [blame] | 1313 | if (LinkTypes(Dest, Src, ErrorMsg)) |
| 1314 | return true; |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 1315 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1316 | // ValueMap - Mapping of values from what they used to be in Src, to what they |
| 1317 | // are now in Dest. |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 1318 | std::map<const Value*, Value*> ValueMap; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1319 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1320 | // AppendingVars - Keep track of global variables in the destination module |
| 1321 | // with appending linkage. After the module is linked together, they are |
| 1322 | // appended and the module is rewritten. |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1323 | std::multimap<std::string, GlobalVariable *> AppendingVars; |
Chris Lattner | 1127315 | 2006-06-16 01:24:04 +0000 | [diff] [blame] | 1324 | for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end(); |
| 1325 | I != E; ++I) { |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 1326 | // Add all of the appending globals already in the Dest module to |
| 1327 | // AppendingVars. |
Chris Lattner | f414646 | 2003-05-14 12:11:51 +0000 | [diff] [blame] | 1328 | if (I->hasAppendingLinkage()) |
| 1329 | AppendingVars.insert(std::make_pair(I->getName(), I)); |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1332 | // Insert all of the globals in src into the Dest module... without linking |
| 1333 | // initializers (which could refer to functions not yet mapped over). |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1334 | if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg)) |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 1335 | return true; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 1337 | // Link the functions together between the two modules, without doing function |
| 1338 | // bodies... this just adds external function prototypes to the Dest |
| 1339 | // function... We do this so that when we begin processing function bodies, |
| 1340 | // all of the global values that may be referenced are available in our |
| 1341 | // ValueMap. |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1342 | if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 1343 | return true; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 1344 | |
Anton Korobeynikov | 4fb2873 | 2008-03-05 15:27:21 +0000 | [diff] [blame] | 1345 | // If there were any alias, link them now. We really need to do this now, |
| 1346 | // because all of the aliases that may be referenced need to be available in |
| 1347 | // ValueMap |
| 1348 | if (LinkAlias(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 1349 | |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 1350 | // Update the initializers in the Dest module now that all globals that may |
| 1351 | // be referenced are in Dest. |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 1352 | if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 1353 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 1354 | // Link in the function bodies that are defined in the source module into the |
| 1355 | // DestModule. This consists basically of copying the function over and |
| 1356 | // fixing up references to values. |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 1357 | if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true; |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1359 | // If there were any appending global variables, link them together now. |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 1360 | if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true; |
| 1361 | |
Anton Korobeynikov | 3db9191 | 2008-03-05 23:08:47 +0000 | [diff] [blame] | 1362 | // Resolve all uses of aliases with aliasees |
| 1363 | if (ResolveAliases(Dest)) return true; |
| 1364 | |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 1365 | // If the source library's module id is in the dependent library list of the |
| 1366 | // destination library, remove it since that module is now linked in. |
| 1367 | sys::Path modId; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 1368 | modId.set(Src->getModuleIdentifier()); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 1369 | if (!modId.isEmpty()) |
| 1370 | Dest->removeLibrary(modId.getBasename()); |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 1371 | |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1372 | return false; |
| 1373 | } |
Vikram S. Adve | 9466f51 | 2001-10-28 21:38:02 +0000 | [diff] [blame] | 1374 | |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 1375 | // vim: sw=2 |