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