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