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