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