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