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