Reid Spencer | 7f49602 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1 | //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the LLVM module linker. |
| 11 | // |
| 12 | // Specifically, this: |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 13 | // * Merges global variables between the two modules |
| 14 | // * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if != |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 15 | // * Merges functions between two modules |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Reid Spencer | 7cc371a | 2004-11-14 23:27:04 +0000 | [diff] [blame] | 19 | #include "llvm/Linker.h" |
Chris Lattner | adbc0b5 | 2003-11-20 18:23:14 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 23 | #include "llvm/SymbolTable.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
Chris Lattner | adbc0b5 | 2003-11-20 18:23:14 +0000 | [diff] [blame] | 25 | #include "llvm/Assembly/Writer.h" |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 26 | #include "llvm/System/Path.h" |
Reid Spencer | c28a224 | 2004-07-04 11:52:49 +0000 | [diff] [blame] | 27 | #include <iostream> |
Chris Lattner | 72cf7df | 2004-08-21 00:50:59 +0000 | [diff] [blame] | 28 | #include <sstream> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 31 | // Error - Simple wrapper function to conditionally assign to E and return true. |
| 32 | // This just makes error return conditions a little bit simpler... |
| 33 | // |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 34 | static inline bool Error(std::string *E, const std::string &Message) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 35 | if (E) *E = Message; |
| 36 | return true; |
| 37 | } |
| 38 | |
Chris Lattner | 72cf7df | 2004-08-21 00:50:59 +0000 | [diff] [blame] | 39 | static std::string ToStr(const Type *Ty, const Module *M) { |
| 40 | std::ostringstream OS; |
| 41 | WriteTypeSymbolic(OS, Ty, M); |
| 42 | return OS.str(); |
| 43 | } |
| 44 | |
John Criswell | 700867b | 2003-11-04 15:22:26 +0000 | [diff] [blame] | 45 | // |
| 46 | // Function: ResolveTypes() |
| 47 | // |
| 48 | // Description: |
| 49 | // Attempt to link the two specified types together. |
| 50 | // |
| 51 | // Inputs: |
| 52 | // DestTy - The type to which we wish to resolve. |
| 53 | // SrcTy - The original type which we want to resolve. |
| 54 | // Name - The name of the type. |
| 55 | // |
| 56 | // Outputs: |
| 57 | // DestST - The symbol table in which the new type should be placed. |
| 58 | // |
| 59 | // Return value: |
| 60 | // true - There is an error and the types cannot yet be linked. |
| 61 | // false - No errors. |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 62 | // |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 63 | static bool ResolveTypes(const Type *DestTy, const Type *SrcTy, |
| 64 | SymbolTable *DestST, const std::string &Name) { |
| 65 | if (DestTy == SrcTy) return false; // If already equal, noop |
| 66 | |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 67 | // Does the type already exist in the module? |
| 68 | if (DestTy && !isa<OpaqueType>(DestTy)) { // Yup, the type already exists... |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 69 | if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) { |
| 70 | const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy); |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 71 | } else { |
| 72 | return true; // Cannot link types... neither is opaque and not-equal |
| 73 | } |
| 74 | } else { // Type not in dest module. Add it now. |
| 75 | if (DestTy) // Type _is_ in module, just opaque... |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 76 | const_cast<OpaqueType*>(cast<OpaqueType>(DestTy)) |
| 77 | ->refineAbstractTypeTo(SrcTy); |
Chris Lattner | fcd0234 | 2003-08-23 20:31:10 +0000 | [diff] [blame] | 78 | else if (!Name.empty()) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 79 | DestST->insert(Name, const_cast<Type*>(SrcTy)); |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 84 | static const FunctionType *getFT(const PATypeHolder &TH) { |
| 85 | return cast<FunctionType>(TH.get()); |
| 86 | } |
Chris Lattner | 9732be7 | 2003-08-22 20:16:48 +0000 | [diff] [blame] | 87 | static const StructType *getST(const PATypeHolder &TH) { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 88 | return cast<StructType>(TH.get()); |
| 89 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 90 | |
| 91 | // RecursiveResolveTypes - This is just like ResolveTypes, except that it |
| 92 | // recurses down into derived types, merging the used types if the parent types |
| 93 | // are compatible. |
| 94 | // |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 95 | static bool RecursiveResolveTypesI(const PATypeHolder &DestTy, |
| 96 | const PATypeHolder &SrcTy, |
| 97 | SymbolTable *DestST, const std::string &Name, |
| 98 | std::vector<std::pair<PATypeHolder, PATypeHolder> > &Pointers) { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 99 | const Type *SrcTyT = SrcTy.get(); |
| 100 | const Type *DestTyT = DestTy.get(); |
| 101 | if (DestTyT == SrcTyT) return false; // If already equal, noop |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 102 | |
| 103 | // If we found our opaque type, resolve it now! |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 104 | if (isa<OpaqueType>(DestTyT) || isa<OpaqueType>(SrcTyT)) |
| 105 | return ResolveTypes(DestTyT, SrcTyT, DestST, Name); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 106 | |
| 107 | // Two types cannot be resolved together if they are of different primitive |
| 108 | // type. For example, we cannot resolve an int to a float. |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 109 | if (DestTyT->getTypeID() != SrcTyT->getTypeID()) return true; |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 110 | |
| 111 | // Otherwise, resolve the used type used by this derived type... |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 112 | switch (DestTyT->getTypeID()) { |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 113 | case Type::FunctionTyID: { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 114 | if (cast<FunctionType>(DestTyT)->isVarArg() != |
Chris Lattner | 841e00b | 2003-08-28 16:42:50 +0000 | [diff] [blame] | 115 | cast<FunctionType>(SrcTyT)->isVarArg() || |
| 116 | cast<FunctionType>(DestTyT)->getNumContainedTypes() != |
| 117 | cast<FunctionType>(SrcTyT)->getNumContainedTypes()) |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 118 | return true; |
| 119 | for (unsigned i = 0, e = getFT(DestTy)->getNumContainedTypes(); i != e; ++i) |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 120 | if (RecursiveResolveTypesI(getFT(DestTy)->getContainedType(i), |
| 121 | getFT(SrcTy)->getContainedType(i), DestST, "", |
| 122 | Pointers)) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 123 | return true; |
| 124 | return false; |
| 125 | } |
| 126 | case Type::StructTyID: { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 127 | if (getST(DestTy)->getNumContainedTypes() != |
| 128 | getST(SrcTy)->getNumContainedTypes()) return 1; |
| 129 | for (unsigned i = 0, e = getST(DestTy)->getNumContainedTypes(); i != e; ++i) |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 130 | if (RecursiveResolveTypesI(getST(DestTy)->getContainedType(i), |
| 131 | getST(SrcTy)->getContainedType(i), DestST, "", |
| 132 | Pointers)) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 133 | return true; |
| 134 | return false; |
| 135 | } |
| 136 | case Type::ArrayTyID: { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 137 | const ArrayType *DAT = cast<ArrayType>(DestTy.get()); |
| 138 | const ArrayType *SAT = cast<ArrayType>(SrcTy.get()); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 139 | if (DAT->getNumElements() != SAT->getNumElements()) return true; |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 140 | return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(), |
| 141 | DestST, "", Pointers); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 143 | case Type::PointerTyID: { |
| 144 | // If this is a pointer type, check to see if we have already seen it. If |
| 145 | // so, we are in a recursive branch. Cut off the search now. We cannot use |
| 146 | // an associative container for this search, because the type pointers (keys |
| 147 | // in the container) change whenever types get resolved... |
| 148 | // |
| 149 | for (unsigned i = 0, e = Pointers.size(); i != e; ++i) |
| 150 | if (Pointers[i].first == DestTy) |
| 151 | return Pointers[i].second != SrcTy; |
| 152 | |
| 153 | // Otherwise, add the current pointers to the vector to stop recursion on |
| 154 | // this pair. |
| 155 | Pointers.push_back(std::make_pair(DestTyT, SrcTyT)); |
| 156 | bool Result = |
| 157 | RecursiveResolveTypesI(cast<PointerType>(DestTy.get())->getElementType(), |
| 158 | cast<PointerType>(SrcTy.get())->getElementType(), |
| 159 | DestST, "", Pointers); |
| 160 | Pointers.pop_back(); |
| 161 | return Result; |
| 162 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 163 | default: assert(0 && "Unexpected type!"); return true; |
| 164 | } |
| 165 | } |
| 166 | |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 167 | static bool RecursiveResolveTypes(const PATypeHolder &DestTy, |
| 168 | const PATypeHolder &SrcTy, |
| 169 | SymbolTable *DestST, const std::string &Name){ |
| 170 | std::vector<std::pair<PATypeHolder, PATypeHolder> > PointerTypes; |
| 171 | return RecursiveResolveTypesI(DestTy, SrcTy, DestST, Name, PointerTypes); |
| 172 | } |
| 173 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 175 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 176 | // types are named in the src module that are not named in the Dst module. |
| 177 | // Make sure there are no type name conflicts. |
| 178 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 179 | static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) { |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 180 | SymbolTable *DestST = &Dest->getSymbolTable(); |
| 181 | const SymbolTable *SrcST = &Src->getSymbolTable(); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 182 | |
| 183 | // Look for a type plane for Type's... |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 184 | SymbolTable::type_const_iterator TI = SrcST->type_begin(); |
| 185 | SymbolTable::type_const_iterator TE = SrcST->type_end(); |
| 186 | if (TI == TE) return false; // No named types, do nothing. |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 187 | |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 188 | // Some types cannot be resolved immediately because they depend on other |
| 189 | // types being resolved to each other first. This contains a list of types we |
| 190 | // are waiting to recheck. |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 191 | std::vector<std::string> DelayedTypesToResolve; |
| 192 | |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 193 | for ( ; TI != TE; ++TI ) { |
| 194 | const std::string &Name = TI->first; |
Reid Spencer | c28a224 | 2004-07-04 11:52:49 +0000 | [diff] [blame] | 195 | const Type *RHS = TI->second; |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 196 | |
| 197 | // Check to see if this type name is already in the dest module... |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 198 | Type *Entry = DestST->lookupType(Name); |
Chris Lattner | 2f6bb2b | 2003-01-30 20:53:43 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 200 | if (ResolveTypes(Entry, RHS, DestST, Name)) { |
| 201 | // They look different, save the types 'till later to resolve. |
| 202 | DelayedTypesToResolve.push_back(Name); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 205 | |
| 206 | // Iteratively resolve types while we can... |
| 207 | while (!DelayedTypesToResolve.empty()) { |
| 208 | // Loop over all of the types, attempting to resolve them if possible... |
| 209 | unsigned OldSize = DelayedTypesToResolve.size(); |
| 210 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 211 | // Try direct resolution by name... |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 212 | for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) { |
| 213 | const std::string &Name = DelayedTypesToResolve[i]; |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 214 | Type *T1 = SrcST->lookupType(Name); |
| 215 | Type *T2 = DestST->lookupType(Name); |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 216 | if (!ResolveTypes(T2, T1, DestST, Name)) { |
| 217 | // We are making progress! |
| 218 | DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); |
| 219 | --i; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Did we not eliminate any types? |
| 224 | if (DelayedTypesToResolve.size() == OldSize) { |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 225 | // Attempt to resolve subelements of types. This allows us to merge these |
| 226 | // two types: { int* } and { opaque* } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 227 | for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) { |
| 228 | const std::string &Name = DelayedTypesToResolve[i]; |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 229 | PATypeHolder T1(SrcST->lookupType(Name)); |
| 230 | PATypeHolder T2(DestST->lookupType(Name)); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 231 | |
| 232 | if (!RecursiveResolveTypes(T2, T1, DestST, Name)) { |
| 233 | // We are making progress! |
| 234 | DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); |
| 235 | |
| 236 | // Go back to the main loop, perhaps we can resolve directly by name |
| 237 | // now... |
| 238 | break; |
| 239 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 240 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 241 | |
| 242 | // If we STILL cannot resolve the types, then there is something wrong. |
Chris Lattner | aeb18ce | 2003-10-21 22:46:38 +0000 | [diff] [blame] | 243 | // Report the warning and delete one of the names. |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 244 | if (DelayedTypesToResolve.size() == OldSize) { |
Chris Lattner | aeb18ce | 2003-10-21 22:46:38 +0000 | [diff] [blame] | 245 | const std::string &Name = DelayedTypesToResolve.back(); |
| 246 | |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 247 | const Type *T1 = SrcST->lookupType(Name); |
| 248 | const Type *T2 = DestST->lookupType(Name); |
Chris Lattner | aeb18ce | 2003-10-21 22:46:38 +0000 | [diff] [blame] | 249 | std::cerr << "WARNING: Type conflict between types named '" << Name |
Chris Lattner | adbc0b5 | 2003-11-20 18:23:14 +0000 | [diff] [blame] | 250 | << "'.\n Src='"; |
| 251 | WriteTypeSymbolic(std::cerr, T1, Src); |
| 252 | std::cerr << "'.\n Dest='"; |
| 253 | WriteTypeSymbolic(std::cerr, T2, Dest); |
| 254 | std::cerr << "'\n"; |
Chris Lattner | aeb18ce | 2003-10-21 22:46:38 +0000 | [diff] [blame] | 255 | |
| 256 | // Remove the symbol name from the destination. |
| 257 | DelayedTypesToResolve.pop_back(); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 258 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 263 | return false; |
| 264 | } |
| 265 | |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 266 | static void PrintMap(const std::map<const Value*, Value*> &M) { |
| 267 | 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] | 268 | I != E; ++I) { |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 269 | std::cerr << " Fr: " << (void*)I->first << " "; |
Chris Lattner | 87182ae | 2002-04-07 22:31:23 +0000 | [diff] [blame] | 270 | I->first->dump(); |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 271 | std::cerr << " To: " << (void*)I->second << " "; |
Chris Lattner | 87182ae | 2002-04-07 22:31:23 +0000 | [diff] [blame] | 272 | I->second->dump(); |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 273 | std::cerr << "\n"; |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 278 | // RemapOperand - Use ValueMap to convert references from one module to another. |
| 279 | // This is somewhat sophisticated in that it can automatically handle constant |
| 280 | // references correctly as well... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 281 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 282 | static Value *RemapOperand(const Value *In, |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 283 | std::map<const Value*, Value*> &ValueMap) { |
| 284 | std::map<const Value*,Value*>::const_iterator I = ValueMap.find(In); |
| 285 | if (I != ValueMap.end()) return I->second; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 287 | // Check to see if it's a constant that we are interesting in transforming. |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 288 | if (const Constant *CPV = dyn_cast<Constant>(In)) { |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 289 | if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) || |
| 290 | isa<ConstantAggregateZero>(CPV)) |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 291 | return const_cast<Constant*>(CPV); // Simple constants stay identical. |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 292 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 293 | Constant *Result = 0; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 295 | if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) { |
Alkis Evlogimenos | cc7ba49 | 2004-08-04 08:08:13 +0000 | [diff] [blame] | 296 | std::vector<Constant*> Operands(CPA->getNumOperands()); |
| 297 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 298 | Operands[i] =cast<Constant>(RemapOperand(CPA->getOperand(i), ValueMap)); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 299 | Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 300 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) { |
Alkis Evlogimenos | cc7ba49 | 2004-08-04 08:08:13 +0000 | [diff] [blame] | 301 | std::vector<Constant*> Operands(CPS->getNumOperands()); |
| 302 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 303 | Operands[i] =cast<Constant>(RemapOperand(CPS->getOperand(i), ValueMap)); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 304 | Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands); |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 305 | } else if (isa<ConstantPointerNull>(CPV) || isa<UndefValue>(CPV)) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 306 | Result = const_cast<Constant*>(CPV); |
Reid Spencer | 00dc479 | 2004-07-17 23:50:57 +0000 | [diff] [blame] | 307 | } else if (isa<GlobalValue>(CPV)) { |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 308 | Result = cast<Constant>(RemapOperand(CPV, ValueMap)); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 309 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 310 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 311 | Value *Ptr = RemapOperand(CE->getOperand(0), ValueMap); |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 312 | std::vector<Constant*> Indices; |
| 313 | Indices.reserve(CE->getNumOperands()-1); |
| 314 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 315 | Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i), |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 316 | ValueMap))); |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 317 | |
| 318 | Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices); |
| 319 | } else if (CE->getNumOperands() == 1) { |
Chris Lattner | ad33348 | 2002-08-14 18:24:09 +0000 | [diff] [blame] | 320 | // Cast instruction |
| 321 | assert(CE->getOpcode() == Instruction::Cast); |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 322 | Value *V = RemapOperand(CE->getOperand(0), ValueMap); |
Chris Lattner | ad33348 | 2002-08-14 18:24:09 +0000 | [diff] [blame] | 323 | Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType()); |
Chris Lattner | 1438102 | 2004-03-31 02:58:28 +0000 | [diff] [blame] | 324 | } else if (CE->getNumOperands() == 3) { |
| 325 | // Select instruction |
| 326 | assert(CE->getOpcode() == Instruction::Select); |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 327 | Value *V1 = RemapOperand(CE->getOperand(0), ValueMap); |
| 328 | Value *V2 = RemapOperand(CE->getOperand(1), ValueMap); |
| 329 | Value *V3 = RemapOperand(CE->getOperand(2), ValueMap); |
Chris Lattner | 1438102 | 2004-03-31 02:58:28 +0000 | [diff] [blame] | 330 | Result = ConstantExpr::getSelect(cast<Constant>(V1), cast<Constant>(V2), |
| 331 | cast<Constant>(V3)); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 332 | } else if (CE->getNumOperands() == 2) { |
| 333 | // Binary operator... |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 334 | Value *V1 = RemapOperand(CE->getOperand(0), ValueMap); |
| 335 | Value *V2 = RemapOperand(CE->getOperand(1), ValueMap); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 336 | |
| 337 | Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1), |
Chris Lattner | d981f8a | 2003-11-05 20:37:01 +0000 | [diff] [blame] | 338 | cast<Constant>(V2)); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 339 | } else { |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 340 | assert(0 && "Unknown constant expr type!"); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 343 | } else { |
| 344 | assert(0 && "Unknown type of derived type constant value!"); |
| 345 | } |
| 346 | |
| 347 | // Cache the mapping in our local map structure... |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 348 | ValueMap.insert(std::make_pair(In, Result)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 349 | return Result; |
| 350 | } |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 352 | std::cerr << "LinkModules ValueMap: \n"; |
| 353 | PrintMap(ValueMap); |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 355 | std::cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n"; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 356 | assert(0 && "Couldn't remap value!"); |
| 357 | return 0; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 360 | /// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict |
| 361 | /// in the symbol table. This is good for all clients except for us. Go |
| 362 | /// through the trouble to force this back. |
| 363 | static void ForceRenaming(GlobalValue *GV, const std::string &Name) { |
| 364 | assert(GV->getName() != Name && "Can't force rename to self"); |
| 365 | SymbolTable &ST = GV->getParent()->getSymbolTable(); |
| 366 | |
| 367 | // If there is a conflict, rename the conflict. |
| 368 | Value *ConflictVal = ST.lookup(GV->getType(), Name); |
| 369 | assert(ConflictVal&&"Why do we have to force rename if there is no conflic?"); |
| 370 | GlobalValue *ConflictGV = cast<GlobalValue>(ConflictVal); |
| 371 | assert(ConflictGV->hasInternalLinkage() && |
| 372 | "Not conflicting with a static global, should link instead!"); |
| 373 | |
| 374 | ConflictGV->setName(""); // Eliminate the conflict |
| 375 | GV->setName(Name); // Force the name back |
| 376 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
Chris Lattner | 7b0c84d | 2004-08-04 07:28:06 +0000 | [diff] [blame] | 377 | assert(GV->getName() == Name && ConflictGV->getName() != Name && |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 378 | "ForceRenaming didn't work"); |
| 379 | } |
| 380 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 381 | |
| 382 | // LinkGlobals - Loop through the global variables in the src module and merge |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 383 | // them into the dest module. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 384 | // |
| 385 | static bool LinkGlobals(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 386 | std::map<const Value*, Value*> &ValueMap, |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 387 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 388 | std::map<std::string, GlobalValue*> &GlobalsByName, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 389 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 390 | // We will need a module level symbol table if the src module has a module |
| 391 | // level symbol table... |
Chris Lattner | b91b657 | 2002-12-03 18:32:30 +0000 | [diff] [blame] | 392 | SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable(); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 393 | |
| 394 | // Loop over all of the globals in the src module, mapping them over as we go |
| 395 | // |
| 396 | for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){ |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 397 | const GlobalVariable *SGV = I; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 398 | GlobalVariable *DGV = 0; |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 399 | // Check to see if may have to link the global. |
Chris Lattner | aad2deb | 2004-08-04 22:39:54 +0000 | [diff] [blame] | 400 | if (SGV->hasName() && !SGV->hasInternalLinkage()) |
| 401 | if (!(DGV = Dest->getGlobalVariable(SGV->getName(), |
| 402 | SGV->getType()->getElementType()))) { |
| 403 | std::map<std::string, GlobalValue*>::iterator EGV = |
| 404 | GlobalsByName.find(SGV->getName()); |
| 405 | if (EGV != GlobalsByName.end()) |
| 406 | DGV = dyn_cast<GlobalVariable>(EGV->second); |
Chris Lattner | a63acbf | 2004-11-16 19:04:40 +0000 | [diff] [blame^] | 407 | if (DGV && RecursiveResolveTypes(SGV->getType(), DGV->getType(),ST, "")) |
Chris Lattner | aad2deb | 2004-08-04 22:39:54 +0000 | [diff] [blame] | 408 | DGV = 0; // FIXME: gross. |
| 409 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 411 | assert(SGV->hasInitializer() || SGV->hasExternalLinkage() && |
| 412 | "Global must either be external or have an initializer!"); |
| 413 | |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 414 | bool SGExtern = SGV->isExternal(); |
| 415 | bool DGExtern = DGV ? DGV->isExternal() : false; |
| 416 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 417 | if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) { |
| 418 | // No linking to be performed, simply create an identical version of the |
| 419 | // symbol over in the dest module... the initializer will be filled in |
| 420 | // later by LinkGlobalInits... |
| 421 | // |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 422 | GlobalVariable *NewDGV = |
| 423 | new GlobalVariable(SGV->getType()->getElementType(), |
| 424 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
| 425 | SGV->getName(), Dest); |
| 426 | |
| 427 | // If the LLVM runtime renamed the global, but it is an externally visible |
| 428 | // symbol, DGV must be an existing global with internal linkage. Rename |
| 429 | // it. |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 430 | if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()) |
| 431 | ForceRenaming(NewDGV, SGV->getName()); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 432 | |
| 433 | // Make sure to remember this mapping... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 434 | ValueMap.insert(std::make_pair(SGV, NewDGV)); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 435 | if (SGV->hasAppendingLinkage()) |
| 436 | // Keep track that this is an appending variable... |
| 437 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
| 438 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 439 | } else if (SGV->isExternal()) { |
| 440 | // If SGV is external or if both SGV & DGV are external.. Just link the |
| 441 | // external globals, we aren't adding anything. |
| 442 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 443 | |
Chris Lattner | d4984cc | 2004-10-05 02:28:11 +0000 | [diff] [blame] | 444 | // Inherit 'const' information. |
| 445 | if (SGV->isConstant()) DGV->setConstant(true); |
| 446 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 447 | } else if (DGV->isExternal()) { // If DGV is external but SGV is not... |
| 448 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 449 | DGV->setLinkage(SGV->getLinkage()); // Inherit linkage! |
Chris Lattner | d4984cc | 2004-10-05 02:28:11 +0000 | [diff] [blame] | 450 | |
| 451 | if (DGV->isConstant() && !SGV->isConstant()) |
| 452 | return Error(Err, "Linking globals named '" + SGV->getName() + |
| 453 | "': declaration is const but definition is not!"); |
| 454 | |
| 455 | // Inherit 'const' information. |
| 456 | if (SGV->isConstant()) DGV->setConstant(true); |
| 457 | |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 458 | } else if (SGV->hasWeakLinkage() || SGV->hasLinkOnceLinkage()) { |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 459 | // At this point we know that DGV has LinkOnce, Appending, Weak, or |
| 460 | // External linkage. If DGV is Appending, this is an error. |
| 461 | if (DGV->hasAppendingLinkage()) |
| 462 | return Error(Err, "Linking globals named '" + SGV->getName() + |
Chris Lattner | d4984cc | 2004-10-05 02:28:11 +0000 | [diff] [blame] | 463 | "' with 'weak' and 'appending' linkage is not allowed!"); |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 464 | |
| 465 | if (SGV->isConstant() != DGV->isConstant()) |
| 466 | return Error(Err, "Global Variable Collision on '" + |
Chris Lattner | 72cf7df | 2004-08-21 00:50:59 +0000 | [diff] [blame] | 467 | ToStr(SGV->getType(), Src) + " %" + SGV->getName() + |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 468 | "' - Global variables differ in const'ness"); |
| 469 | |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 470 | // Otherwise, just perform the link. |
| 471 | ValueMap.insert(std::make_pair(SGV, DGV)); |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 472 | |
| 473 | // Linkonce+Weak = Weak |
| 474 | if (DGV->hasLinkOnceLinkage() && SGV->hasWeakLinkage()) |
| 475 | DGV->setLinkage(SGV->getLinkage()); |
| 476 | |
| 477 | } else if (DGV->hasWeakLinkage() || DGV->hasLinkOnceLinkage()) { |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 478 | // At this point we know that SGV has LinkOnce, Appending, or External |
| 479 | // linkage. If SGV is Appending, this is an error. |
| 480 | if (SGV->hasAppendingLinkage()) |
| 481 | return Error(Err, "Linking globals named '" + SGV->getName() + |
| 482 | " ' with 'weak' and 'appending' linkage is not allowed!"); |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 483 | |
| 484 | if (SGV->isConstant() != DGV->isConstant()) |
| 485 | return Error(Err, "Global Variable Collision on '" + |
Chris Lattner | 72cf7df | 2004-08-21 00:50:59 +0000 | [diff] [blame] | 486 | ToStr(SGV->getType(), Src) + " %" + SGV->getName() + |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 487 | "' - Global variables differ in const'ness"); |
| 488 | |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 489 | if (!SGV->hasLinkOnceLinkage()) |
| 490 | DGV->setLinkage(SGV->getLinkage()); // Inherit linkage! |
| 491 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 492 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 493 | } else if (SGV->getLinkage() != DGV->getLinkage()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 494 | return Error(Err, "Global variables named '" + SGV->getName() + |
| 495 | "' have different linkage specifiers!"); |
Chris Lattner | d4984cc | 2004-10-05 02:28:11 +0000 | [diff] [blame] | 496 | // Inherit 'const' information. |
| 497 | if (SGV->isConstant()) DGV->setConstant(true); |
| 498 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 499 | } else if (SGV->hasExternalLinkage()) { |
| 500 | // Allow linking two exactly identical external global variables... |
Chris Lattner | f85770c | 2003-10-21 21:52:20 +0000 | [diff] [blame] | 501 | if (SGV->isConstant() != DGV->isConstant()) |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 502 | return Error(Err, "Global Variable Collision on '" + |
Chris Lattner | 72cf7df | 2004-08-21 00:50:59 +0000 | [diff] [blame] | 503 | ToStr(SGV->getType(), Src) + " %" + SGV->getName() + |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 504 | "' - Global variables differ in const'ness"); |
Chris Lattner | f85770c | 2003-10-21 21:52:20 +0000 | [diff] [blame] | 505 | |
| 506 | if (SGV->getInitializer() != DGV->getInitializer()) |
| 507 | return Error(Err, "Global Variable Collision on '" + |
Chris Lattner | 72cf7df | 2004-08-21 00:50:59 +0000 | [diff] [blame] | 508 | ToStr(SGV->getType(), Src) + " %" + SGV->getName() + |
Chris Lattner | f85770c | 2003-10-21 21:52:20 +0000 | [diff] [blame] | 509 | "' - External linkage globals have different initializers"); |
| 510 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 511 | ValueMap.insert(std::make_pair(SGV, DGV)); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 512 | } else if (SGV->hasAppendingLinkage()) { |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 513 | // No linking is performed yet. Just insert a new copy of the global, and |
| 514 | // keep track of the fact that it is an appending variable in the |
| 515 | // AppendingVars map. The name is cleared out so that no linkage is |
| 516 | // performed. |
| 517 | GlobalVariable *NewDGV = |
| 518 | new GlobalVariable(SGV->getType()->getElementType(), |
| 519 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
| 520 | "", Dest); |
| 521 | |
| 522 | // Make sure to remember this mapping... |
| 523 | ValueMap.insert(std::make_pair(SGV, NewDGV)); |
| 524 | |
| 525 | // Keep track that this is an appending variable... |
| 526 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 527 | } else { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 528 | assert(0 && "Unknown linkage!"); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 535 | // LinkGlobalInits - Update the initializers in the Dest module now that all |
| 536 | // globals that may be referenced are in Dest. |
| 537 | // |
| 538 | static bool LinkGlobalInits(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 539 | std::map<const Value*, Value*> &ValueMap, |
| 540 | std::string *Err) { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 541 | |
| 542 | // Loop over all of the globals in the src module, mapping them over as we go |
| 543 | // |
| 544 | for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){ |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 545 | const GlobalVariable *SGV = I; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 546 | |
| 547 | if (SGV->hasInitializer()) { // Only process initialized GV's |
| 548 | // Figure out what the initializer looks like in the dest module... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 549 | Constant *SInit = |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 550 | cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 551 | |
| 552 | GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 553 | if (DGV->hasInitializer()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 554 | if (SGV->hasExternalLinkage()) { |
| 555 | if (DGV->getInitializer() != SInit) |
| 556 | return Error(Err, "Global Variable Collision on '" + |
Chris Lattner | 72cf7df | 2004-08-21 00:50:59 +0000 | [diff] [blame] | 557 | ToStr(SGV->getType(), Src) +"':%"+SGV->getName()+ |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 558 | " - Global variables have different initializers"); |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 559 | } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 560 | // Nothing is required, mapped values will take the new global |
| 561 | // automatically. |
Chris Lattner | 57cb988 | 2004-02-17 21:56:04 +0000 | [diff] [blame] | 562 | } else if (SGV->hasLinkOnceLinkage() || SGV->hasWeakLinkage()) { |
| 563 | // Nothing is required, mapped values will take the new global |
| 564 | // automatically. |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 565 | } else if (DGV->hasAppendingLinkage()) { |
| 566 | assert(0 && "Appending linkage unimplemented!"); |
| 567 | } else { |
| 568 | assert(0 && "Unknown linkage!"); |
| 569 | } |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 570 | } else { |
| 571 | // Copy the initializer over now... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 572 | DGV->setInitializer(SInit); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | } |
| 576 | return false; |
| 577 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 578 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 579 | // LinkFunctionProtos - Link the functions together between the two modules, |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 580 | // without doing function bodies... this just adds external function prototypes |
| 581 | // to the Dest function... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 582 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 583 | static bool LinkFunctionProtos(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 584 | std::map<const Value*, Value*> &ValueMap, |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 585 | std::map<std::string, GlobalValue*> &GlobalsByName, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 586 | std::string *Err) { |
Chris Lattner | b91b657 | 2002-12-03 18:32:30 +0000 | [diff] [blame] | 587 | SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable(); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 588 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 589 | // Loop over all of the functions in the src module, mapping them over as we |
| 590 | // go |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 591 | // |
| 592 | 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] | 593 | const Function *SF = I; // SrcFunction |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 594 | Function *DF = 0; |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 595 | if (SF->hasName() && !SF->hasInternalLinkage()) { |
| 596 | // Check to see if may have to link the function. |
Chris Lattner | aad2deb | 2004-08-04 22:39:54 +0000 | [diff] [blame] | 597 | if (!(DF = Dest->getFunction(SF->getName(), SF->getFunctionType()))) { |
| 598 | std::map<std::string, GlobalValue*>::iterator EF = |
| 599 | GlobalsByName.find(SF->getName()); |
| 600 | if (EF != GlobalsByName.end()) |
| 601 | DF = dyn_cast<Function>(EF->second); |
| 602 | if (DF && RecursiveResolveTypes(SF->getType(), DF->getType(), ST, "")) |
| 603 | DF = 0; // FIXME: gross. |
| 604 | } |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 605 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 607 | if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) { |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 608 | // Function does not already exist, simply insert an function signature |
| 609 | // identical to SF into the dest module... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 610 | Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(), |
| 611 | SF->getName(), Dest); |
| 612 | |
| 613 | // If the LLVM runtime renamed the function, but it is an externally |
| 614 | // visible symbol, DF must be an existing function with internal linkage. |
| 615 | // Rename it. |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 616 | if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) |
Chris Lattner | 82b5b21 | 2004-08-04 22:29:05 +0000 | [diff] [blame] | 617 | ForceRenaming(NewDF, SF->getName()); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 618 | |
| 619 | // ... and remember this mapping... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 620 | ValueMap.insert(std::make_pair(SF, NewDF)); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 621 | } else if (SF->isExternal()) { |
| 622 | // If SF is external or if both SF & DF are external.. Just link the |
| 623 | // external functions, we aren't adding anything. |
| 624 | ValueMap.insert(std::make_pair(SF, DF)); |
| 625 | } else if (DF->isExternal()) { // If DF is external but SF is not... |
| 626 | // Link the external functions, update linkage qualifiers |
| 627 | ValueMap.insert(std::make_pair(SF, DF)); |
| 628 | DF->setLinkage(SF->getLinkage()); |
| 629 | |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 630 | } else if (SF->hasWeakLinkage() || SF->hasLinkOnceLinkage()) { |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 631 | // At this point we know that DF has LinkOnce, Weak, or External linkage. |
| 632 | ValueMap.insert(std::make_pair(SF, DF)); |
| 633 | |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 634 | // Linkonce+Weak = Weak |
| 635 | if (DF->hasLinkOnceLinkage() && SF->hasWeakLinkage()) |
| 636 | DF->setLinkage(SF->getLinkage()); |
| 637 | |
| 638 | } else if (DF->hasWeakLinkage() || DF->hasLinkOnceLinkage()) { |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 639 | // At this point we know that SF has LinkOnce or External linkage. |
| 640 | ValueMap.insert(std::make_pair(SF, DF)); |
| 641 | if (!SF->hasLinkOnceLinkage()) // Don't inherit linkonce linkage |
| 642 | DF->setLinkage(SF->getLinkage()); |
| 643 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 644 | } else if (SF->getLinkage() != DF->getLinkage()) { |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 645 | return Error(Err, "Functions named '" + SF->getName() + |
| 646 | "' have different linkage specifiers!"); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 647 | } else if (SF->hasExternalLinkage()) { |
| 648 | // The function is defined in both modules!! |
| 649 | return Error(Err, "Function '" + |
Chris Lattner | 72cf7df | 2004-08-21 00:50:59 +0000 | [diff] [blame] | 650 | ToStr(SF->getFunctionType(), Src) + "':\"" + |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 651 | SF->getName() + "\" - Function is already defined!"); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 652 | } else { |
| 653 | assert(0 && "Unknown linkage configuration found!"); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | return false; |
| 657 | } |
| 658 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 659 | // LinkFunctionBody - Copy the source function over into the dest function and |
| 660 | // fix up references to values. At this point we know that Dest is an external |
| 661 | // function, and that Src is not. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 662 | // |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 663 | static bool LinkFunctionBody(Function *Dest, Function *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 664 | std::map<const Value*, Value*> &GlobalMap, |
| 665 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 666 | assert(Src && Dest && Dest->isExternal() && !Src->isExternal()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 668 | // Go through and convert function arguments over, remembering the mapping. |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 669 | Function::aiterator DI = Dest->abegin(); |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 670 | for (Function::aiterator I = Src->abegin(), E = Src->aend(); |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 671 | I != E; ++I, ++DI) { |
| 672 | DI->setName(I->getName()); // Copy the name information over... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 673 | |
| 674 | // Add a mapping to our local map |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 675 | GlobalMap.insert(std::make_pair(I, DI)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 678 | // Splice the body of the source function into the dest function. |
| 679 | Dest->getBasicBlockList().splice(Dest->end(), Src->getBasicBlockList()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 680 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 681 | // At this point, all of the instructions and values of the function are now |
| 682 | // copied over. The only problem is that they are still referencing values in |
| 683 | // the Source function as operands. Loop through all of the operands of the |
| 684 | // functions and patch them up to point to the local versions... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 685 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 686 | for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB) |
| 687 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 688 | for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 689 | OI != OE; ++OI) |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 690 | if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI)) |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 691 | *OI = RemapOperand(*OI, GlobalMap); |
| 692 | |
| 693 | // There is no need to map the arguments anymore. |
Chris Lattner | a63acbf | 2004-11-16 19:04:40 +0000 | [diff] [blame^] | 694 | for (Function::aiterator I = Src->abegin(), E = Src->aend(); I != E; ++I) |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 695 | GlobalMap.erase(I); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 696 | |
| 697 | return false; |
| 698 | } |
| 699 | |
| 700 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 701 | // LinkFunctionBodies - Link in the function bodies that are defined in the |
| 702 | // source module into the DestModule. This consists basically of copying the |
| 703 | // function over and fixing up references to values. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 704 | // |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 705 | static bool LinkFunctionBodies(Module *Dest, Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 706 | std::map<const Value*, Value*> &ValueMap, |
| 707 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 708 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 709 | // Loop over all of the functions in the src module, mapping them over as we |
| 710 | // go |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 711 | // |
Chris Lattner | 4bbfbff | 2004-11-16 07:31:51 +0000 | [diff] [blame] | 712 | for (Module::iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 713 | if (!SF->isExternal()) { // No body if function is external |
| 714 | Function *DF = cast<Function>(ValueMap[SF]); // Destination function |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 715 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 716 | // DF not external SF external? |
Chris Lattner | 3595655 | 2003-10-27 16:39:39 +0000 | [diff] [blame] | 717 | if (DF->isExternal()) { |
| 718 | // Only provide the function body if there isn't one already. |
| 719 | if (LinkFunctionBody(DF, SF, ValueMap, Err)) |
| 720 | return true; |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 721 | } |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 722 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 723 | } |
| 724 | return false; |
| 725 | } |
| 726 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 727 | // LinkAppendingVars - If there were any appending global variables, link them |
| 728 | // together now. Return true on error. |
| 729 | // |
| 730 | static bool LinkAppendingVars(Module *M, |
| 731 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
| 732 | std::string *ErrorMsg) { |
| 733 | if (AppendingVars.empty()) return false; // Nothing to do. |
| 734 | |
| 735 | // Loop over the multimap of appending vars, processing any variables with the |
| 736 | // same name, forming a new appending global variable with both of the |
| 737 | // initializers merged together, then rewrite references to the old variables |
| 738 | // and delete them. |
| 739 | // |
| 740 | std::vector<Constant*> Inits; |
| 741 | while (AppendingVars.size() > 1) { |
| 742 | // Get the first two elements in the map... |
| 743 | std::multimap<std::string, |
| 744 | GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++; |
| 745 | |
| 746 | // If the first two elements are for different names, there is no pair... |
| 747 | // Otherwise there is a pair, so link them together... |
| 748 | if (First->first == Second->first) { |
| 749 | GlobalVariable *G1 = First->second, *G2 = Second->second; |
| 750 | const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType()); |
| 751 | const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType()); |
| 752 | |
| 753 | // Check to see that they two arrays agree on type... |
| 754 | if (T1->getElementType() != T2->getElementType()) |
| 755 | return Error(ErrorMsg, |
| 756 | "Appending variables with different element types need to be linked!"); |
| 757 | if (G1->isConstant() != G2->isConstant()) |
| 758 | return Error(ErrorMsg, |
| 759 | "Appending variables linked with different const'ness!"); |
| 760 | |
| 761 | unsigned NewSize = T1->getNumElements() + T2->getNumElements(); |
| 762 | ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize); |
| 763 | |
| 764 | // Create the new global variable... |
| 765 | GlobalVariable *NG = |
| 766 | new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(), |
| 767 | /*init*/0, First->first, M); |
| 768 | |
| 769 | // Merge the initializer... |
| 770 | Inits.reserve(NewSize); |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 771 | if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) { |
| 772 | for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) |
Alkis Evlogimenos | cc7ba49 | 2004-08-04 08:08:13 +0000 | [diff] [blame] | 773 | Inits.push_back(I->getOperand(i)); |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 774 | } else { |
| 775 | assert(isa<ConstantAggregateZero>(G1->getInitializer())); |
| 776 | Constant *CV = Constant::getNullValue(T1->getElementType()); |
| 777 | for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) |
| 778 | Inits.push_back(CV); |
| 779 | } |
| 780 | if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) { |
| 781 | for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) |
Alkis Evlogimenos | cc7ba49 | 2004-08-04 08:08:13 +0000 | [diff] [blame] | 782 | Inits.push_back(I->getOperand(i)); |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 783 | } else { |
| 784 | assert(isa<ConstantAggregateZero>(G2->getInitializer())); |
| 785 | Constant *CV = Constant::getNullValue(T2->getElementType()); |
| 786 | for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) |
| 787 | Inits.push_back(CV); |
| 788 | } |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 789 | NG->setInitializer(ConstantArray::get(NewType, Inits)); |
| 790 | Inits.clear(); |
| 791 | |
| 792 | // Replace any uses of the two global variables with uses of the new |
| 793 | // global... |
| 794 | |
| 795 | // FIXME: This should rewrite simple/straight-forward uses such as |
| 796 | // getelementptr instructions to not use the Cast! |
Reid Spencer | 00dc479 | 2004-07-17 23:50:57 +0000 | [diff] [blame] | 797 | G1->replaceAllUsesWith(ConstantExpr::getCast(NG, G1->getType())); |
| 798 | G2->replaceAllUsesWith(ConstantExpr::getCast(NG, G2->getType())); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 799 | |
| 800 | // Remove the two globals from the module now... |
| 801 | M->getGlobalList().erase(G1); |
| 802 | M->getGlobalList().erase(G2); |
| 803 | |
| 804 | // Put the new global into the AppendingVars map so that we can handle |
| 805 | // linking of more than two vars... |
| 806 | Second->second = NG; |
| 807 | } |
| 808 | AppendingVars.erase(First); |
| 809 | } |
| 810 | |
| 811 | return false; |
| 812 | } |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 813 | |
| 814 | |
| 815 | // LinkModules - This function links two modules together, with the resulting |
| 816 | // left module modified to be the composite of the two input modules. If an |
| 817 | // 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] | 818 | // the problem. Upon failure, the Dest module could be in a modified state, and |
| 819 | // shouldn't be relied on to be consistent. |
Chris Lattner | 242e525 | 2004-11-16 06:41:36 +0000 | [diff] [blame] | 820 | bool llvm::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 821 | assert(Dest != 0 && "Invalid Destination module"); |
| 822 | assert(Src != 0 && "Invalid Source Module"); |
| 823 | |
Chris Lattner | 873c5e7 | 2003-08-24 19:26:42 +0000 | [diff] [blame] | 824 | if (Dest->getEndianness() == Module::AnyEndianness) |
| 825 | Dest->setEndianness(Src->getEndianness()); |
| 826 | if (Dest->getPointerSize() == Module::AnyPointerSize) |
| 827 | Dest->setPointerSize(Src->getPointerSize()); |
| 828 | |
| 829 | if (Src->getEndianness() != Module::AnyEndianness && |
| 830 | Dest->getEndianness() != Src->getEndianness()) |
Chris Lattner | 43a9994 | 2003-04-22 19:13:20 +0000 | [diff] [blame] | 831 | std::cerr << "WARNING: Linking two modules of different endianness!\n"; |
Chris Lattner | 873c5e7 | 2003-08-24 19:26:42 +0000 | [diff] [blame] | 832 | if (Src->getPointerSize() != Module::AnyPointerSize && |
| 833 | Dest->getPointerSize() != Src->getPointerSize()) |
Chris Lattner | 43a9994 | 2003-04-22 19:13:20 +0000 | [diff] [blame] | 834 | std::cerr << "WARNING: Linking two modules of different pointer size!\n"; |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 835 | |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 836 | // Update the destination module's dependent libraries list with the libraries |
| 837 | // from the source module. There's no opportunity for duplicates here as the |
| 838 | // Module ensures that duplicate insertions are discarded. |
| 839 | Module::lib_iterator SI = Src->lib_begin(); |
| 840 | Module::lib_iterator SE = Src->lib_end(); |
| 841 | while ( SI != SE ) { |
| 842 | Dest->addLibrary(*SI); |
| 843 | ++SI; |
| 844 | } |
| 845 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 846 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 847 | // types are named in the src module that are not named in the Dst module. |
| 848 | // Make sure there are no type name conflicts. |
| 849 | // |
| 850 | if (LinkTypes(Dest, Src, ErrorMsg)) return true; |
| 851 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 852 | // ValueMap - Mapping of values from what they used to be in Src, to what they |
| 853 | // are now in Dest. |
| 854 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 855 | std::map<const Value*, Value*> ValueMap; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 856 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 857 | // AppendingVars - Keep track of global variables in the destination module |
| 858 | // with appending linkage. After the module is linked together, they are |
| 859 | // appended and the module is rewritten. |
| 860 | // |
| 861 | std::multimap<std::string, GlobalVariable *> AppendingVars; |
| 862 | |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 863 | // GlobalsByName - The LLVM SymbolTable class fights our best efforts at |
| 864 | // linking by separating globals by type. Until PR411 is fixed, we replicate |
| 865 | // it's functionality here. |
| 866 | std::map<std::string, GlobalValue*> GlobalsByName; |
| 867 | |
| 868 | for (Module::giterator I = Dest->gbegin(), E = Dest->gend(); I != E; ++I) { |
| 869 | // Add all of the appending globals already in the Dest module to |
| 870 | // AppendingVars. |
Chris Lattner | f414646 | 2003-05-14 12:11:51 +0000 | [diff] [blame] | 871 | if (I->hasAppendingLinkage()) |
| 872 | AppendingVars.insert(std::make_pair(I->getName(), I)); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 873 | |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 874 | // Keep track of all globals by name. |
| 875 | if (!I->hasInternalLinkage() && I->hasName()) |
| 876 | GlobalsByName[I->getName()] = I; |
| 877 | } |
| 878 | |
| 879 | // Keep track of all globals by name. |
| 880 | for (Module::iterator I = Dest->begin(), E = Dest->end(); I != E; ++I) |
| 881 | if (!I->hasInternalLinkage() && I->hasName()) |
| 882 | GlobalsByName[I->getName()] = I; |
| 883 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 884 | // Insert all of the globals in src into the Dest module... without linking |
| 885 | // initializers (which could refer to functions not yet mapped over). |
| 886 | // |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 887 | if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, GlobalsByName, ErrorMsg)) |
| 888 | return true; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 889 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 890 | // Link the functions together between the two modules, without doing function |
| 891 | // bodies... this just adds external function prototypes to the Dest |
| 892 | // function... We do this so that when we begin processing function bodies, |
| 893 | // all of the global values that may be referenced are available in our |
| 894 | // ValueMap. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 895 | // |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 896 | if (LinkFunctionProtos(Dest, Src, ValueMap, GlobalsByName, ErrorMsg)) |
| 897 | return true; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 899 | // Update the initializers in the Dest module now that all globals that may |
| 900 | // be referenced are in Dest. |
| 901 | // |
| 902 | if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 903 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 904 | // Link in the function bodies that are defined in the source module into the |
| 905 | // DestModule. This consists basically of copying the function over and |
| 906 | // fixing up references to values. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 907 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 908 | if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true; |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 909 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 910 | // If there were any appending global variables, link them together now. |
| 911 | // |
| 912 | if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true; |
| 913 | |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 914 | // If the source library's module id is in the dependent library list of the |
| 915 | // destination library, remove it since that module is now linked in. |
| 916 | sys::Path modId; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 917 | modId.setFile(Src->getModuleIdentifier()); |
| 918 | if (!modId.isEmpty()) |
| 919 | Dest->removeLibrary(modId.getBasename()); |
Reid Spencer | 57a0efa | 2004-09-11 04:25:17 +0000 | [diff] [blame] | 920 | |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 921 | return false; |
| 922 | } |
Vikram S. Adve | 9466f51 | 2001-10-28 21:38:02 +0000 | [diff] [blame] | 923 | |
Reid Spencer | 567bc2c | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 924 | // vim: sw=2 |