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