Mikhail Glushenkov | 59a5afa | 2009-03-03 10:04:23 +0000 | [diff] [blame] | 1 | //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM module linker. |
| 11 | // |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 6cc07df | 2014-03-06 03:42:23 +0000 | [diff] [blame] | 14 | #include "llvm/Linker/Linker.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm-c/Linker.h" |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Optional.h" |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SetVector.h" |
Eli Bendersky | 0f7fd36 | 2013-03-19 15:26:24 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
Chandler Carruth | dcb603f | 2013-01-07 15:43:51 +0000 | [diff] [blame] | 21 | #include "llvm/IR/TypeFinder.h" |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Cloning.h" |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 26 | #include <cctype> |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 29 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // TypeMap implementation. |
| 32 | //===----------------------------------------------------------------------===// |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 33 | |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 34 | namespace { |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 35 | typedef SmallPtrSet<StructType*, 32> TypeSet; |
| 36 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 37 | class TypeMapTy : public ValueMapTypeRemapper { |
| 38 | /// MappedTypes - This is a mapping from a source type to a destination type |
| 39 | /// to use. |
| 40 | DenseMap<Type*, Type*> MappedTypes; |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 41 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 42 | /// SpeculativeTypes - When checking to see if two subgraphs are isomorphic, |
| 43 | /// we speculatively add types to MappedTypes, but keep track of them here in |
| 44 | /// case we need to roll back. |
| 45 | SmallVector<Type*, 16> SpeculativeTypes; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 47 | /// SrcDefinitionsToResolve - This is a list of non-opaque structs in the |
| 48 | /// source module that are mapped to an opaque struct in the destination |
| 49 | /// module. |
| 50 | SmallVector<StructType*, 16> SrcDefinitionsToResolve; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 52 | /// DstResolvedOpaqueTypes - This is the set of opaque types in the |
| 53 | /// destination modules who are getting a body from the source module. |
| 54 | SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes; |
Bill Wendling | 8c2cc41 | 2012-03-22 20:30:41 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 56cdea6 | 2008-06-16 23:06:51 +0000 | [diff] [blame] | 56 | public: |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 57 | TypeMapTy(TypeSet &Set) : DstStructTypesSet(Set) {} |
| 58 | |
| 59 | TypeSet &DstStructTypesSet; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 60 | /// addTypeMapping - Indicate that the specified type in the destination |
| 61 | /// module is conceptually equivalent to the specified type in the source |
| 62 | /// module. |
| 63 | void addTypeMapping(Type *DstTy, Type *SrcTy); |
| 64 | |
| 65 | /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest |
| 66 | /// module from a type definition in the source module. |
| 67 | void linkDefinedTypeBodies(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 68 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 69 | /// get - Return the mapped type to use for the specified input type from the |
| 70 | /// source module. |
| 71 | Type *get(Type *SrcTy); |
| 72 | |
| 73 | FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));} |
| 74 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 75 | /// dump - Dump out the type map for debugging purposes. |
| 76 | void dump() const { |
| 77 | for (DenseMap<Type*, Type*>::const_iterator |
| 78 | I = MappedTypes.begin(), E = MappedTypes.end(); I != E; ++I) { |
| 79 | dbgs() << "TypeMap: "; |
| 80 | I->first->dump(); |
| 81 | dbgs() << " => "; |
| 82 | I->second->dump(); |
| 83 | dbgs() << '\n'; |
| 84 | } |
| 85 | } |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 86 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 87 | private: |
| 88 | Type *getImpl(Type *T); |
| 89 | /// remapType - Implement the ValueMapTypeRemapper interface. |
Craig Topper | 8548299 | 2014-03-05 07:52:44 +0000 | [diff] [blame] | 90 | Type *remapType(Type *SrcTy) override { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 91 | return get(SrcTy); |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 92 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 93 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 94 | bool areTypesIsomorphic(Type *DstTy, Type *SrcTy); |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 95 | }; |
| 96 | } |
| 97 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 98 | void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) { |
| 99 | Type *&Entry = MappedTypes[SrcTy]; |
| 100 | if (Entry) return; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 101 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 102 | if (DstTy == SrcTy) { |
| 103 | Entry = DstTy; |
| 104 | return; |
| 105 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 106 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 107 | // Check to see if these types are recursively isomorphic and establish a |
| 108 | // mapping between them if so. |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 109 | if (!areTypesIsomorphic(DstTy, SrcTy)) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 110 | // Oops, they aren't isomorphic. Just discard this request by rolling out |
| 111 | // any speculative mappings we've established. |
| 112 | for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i) |
| 113 | MappedTypes.erase(SpeculativeTypes[i]); |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 114 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 115 | SpeculativeTypes.clear(); |
| 116 | } |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 117 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 118 | /// areTypesIsomorphic - Recursively walk this pair of types, returning true |
| 119 | /// if they are isomorphic, false if they are not. |
| 120 | bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) { |
| 121 | // Two types with differing kinds are clearly not isomorphic. |
| 122 | if (DstTy->getTypeID() != SrcTy->getTypeID()) return false; |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 123 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 124 | // If we have an entry in the MappedTypes table, then we have our answer. |
| 125 | Type *&Entry = MappedTypes[SrcTy]; |
| 126 | if (Entry) |
| 127 | return Entry == DstTy; |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 128 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 129 | // Two identical types are clearly isomorphic. Remember this |
| 130 | // non-speculatively. |
| 131 | if (DstTy == SrcTy) { |
| 132 | Entry = DstTy; |
Chris Lattner | fe677e9 | 2008-06-16 20:03:01 +0000 | [diff] [blame] | 133 | return true; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 134 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 135 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 136 | // Okay, we have two types with identical kinds that we haven't seen before. |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 137 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 138 | // If this is an opaque struct type, special case it. |
| 139 | if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) { |
| 140 | // Mapping an opaque type to any struct, just keep the dest struct. |
| 141 | if (SSTy->isOpaque()) { |
| 142 | Entry = DstTy; |
| 143 | SpeculativeTypes.push_back(SrcTy); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 144 | return true; |
Chris Lattner | 9be1589 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 147 | // Mapping a non-opaque source type to an opaque dest. If this is the first |
| 148 | // type that we're mapping onto this destination type then we succeed. Keep |
| 149 | // the dest, but fill it in later. This doesn't need to be speculative. If |
| 150 | // this is the second (different) type that we're trying to map onto the |
| 151 | // same opaque type then we fail. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 152 | if (cast<StructType>(DstTy)->isOpaque()) { |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 153 | // We can only map one source type onto the opaque destination type. |
| 154 | if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy))) |
| 155 | return false; |
| 156 | SrcDefinitionsToResolve.push_back(SSTy); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 157 | Entry = DstTy; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 161 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 162 | // If the number of subtypes disagree between the two types, then we fail. |
| 163 | if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes()) |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 164 | return false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 165 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 166 | // Fail if any of the extra properties (e.g. array size) of the type disagree. |
| 167 | if (isa<IntegerType>(DstTy)) |
| 168 | return false; // bitwidth disagrees. |
| 169 | if (PointerType *PT = dyn_cast<PointerType>(DstTy)) { |
| 170 | if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace()) |
| 171 | return false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 172 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 173 | } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) { |
| 174 | if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg()) |
| 175 | return false; |
| 176 | } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) { |
| 177 | StructType *SSTy = cast<StructType>(SrcTy); |
Chris Lattner | 44f7ab4 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 178 | if (DSTy->isLiteral() != SSTy->isLiteral() || |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 179 | DSTy->isPacked() != SSTy->isPacked()) |
| 180 | return false; |
| 181 | } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) { |
| 182 | if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements()) |
| 183 | return false; |
| 184 | } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
Joey Gouly | 5fad3e9 | 2013-01-10 10:49:36 +0000 | [diff] [blame] | 185 | if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 186 | return false; |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 188 | |
| 189 | // Otherwise, we speculate that these two types will line up and recursively |
| 190 | // check the subelements. |
| 191 | Entry = DstTy; |
| 192 | SpeculativeTypes.push_back(SrcTy); |
| 193 | |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 194 | for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i) |
| 195 | if (!areTypesIsomorphic(DstTy->getContainedType(i), |
| 196 | SrcTy->getContainedType(i))) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 197 | return false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 198 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 199 | // If everything seems to have lined up, then everything is great. |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest |
| 204 | /// module from a type definition in the source module. |
| 205 | void TypeMapTy::linkDefinedTypeBodies() { |
| 206 | SmallVector<Type*, 16> Elements; |
| 207 | SmallString<16> TmpName; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 208 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 209 | // Note that processing entries in this loop (calling 'get') can add new |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 210 | // entries to the SrcDefinitionsToResolve vector. |
| 211 | while (!SrcDefinitionsToResolve.empty()) { |
| 212 | StructType *SrcSTy = SrcDefinitionsToResolve.pop_back_val(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 213 | StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 214 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 215 | // TypeMap is a many-to-one mapping, if there were multiple types that |
| 216 | // provide a body for DstSTy then previous iterations of this loop may have |
| 217 | // already handled it. Just ignore this case. |
| 218 | if (!DstSTy->isOpaque()) continue; |
| 219 | assert(!SrcSTy->isOpaque() && "Not resolving a definition?"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 220 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 221 | // Map the body of the source type over to a new body for the dest type. |
| 222 | Elements.resize(SrcSTy->getNumElements()); |
| 223 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) |
| 224 | Elements[i] = getImpl(SrcSTy->getElementType(i)); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 225 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 226 | DstSTy->setBody(Elements, SrcSTy->isPacked()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 227 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 228 | // If DstSTy has no name or has a longer name than STy, then viciously steal |
| 229 | // STy's name. |
| 230 | if (!SrcSTy->hasName()) continue; |
| 231 | StringRef SrcName = SrcSTy->getName(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 232 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 233 | if (!DstSTy->hasName() || DstSTy->getName().size() > SrcName.size()) { |
| 234 | TmpName.insert(TmpName.end(), SrcName.begin(), SrcName.end()); |
| 235 | SrcSTy->setName(""); |
| 236 | DstSTy->setName(TmpName.str()); |
| 237 | TmpName.clear(); |
| 238 | } |
| 239 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 241 | DstResolvedOpaqueTypes.clear(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 244 | /// get - Return the mapped type to use for the specified input type from the |
| 245 | /// source module. |
| 246 | Type *TypeMapTy::get(Type *Ty) { |
| 247 | Type *Result = getImpl(Ty); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 248 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 249 | // If this caused a reference to any struct type, resolve it before returning. |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 250 | if (!SrcDefinitionsToResolve.empty()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 251 | linkDefinedTypeBodies(); |
| 252 | return Result; |
| 253 | } |
| 254 | |
| 255 | /// getImpl - This is the recursive version of get(). |
| 256 | Type *TypeMapTy::getImpl(Type *Ty) { |
| 257 | // If we already have an entry for this type, return it. |
| 258 | Type **Entry = &MappedTypes[Ty]; |
| 259 | if (*Entry) return *Entry; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 260 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 261 | // If this is not a named struct type, then just map all of the elements and |
| 262 | // then rebuild the type from inside out. |
Chris Lattner | 44f7ab4 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 263 | if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 264 | // If there are no element types to map, then the type is itself. This is |
| 265 | // true for the anonymous {} struct, things like 'float', integers, etc. |
| 266 | if (Ty->getNumContainedTypes() == 0) |
| 267 | return *Entry = Ty; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 268 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 269 | // Remap all of the elements, keeping track of whether any of them change. |
| 270 | bool AnyChange = false; |
| 271 | SmallVector<Type*, 4> ElementTypes; |
| 272 | ElementTypes.resize(Ty->getNumContainedTypes()); |
| 273 | for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i) { |
| 274 | ElementTypes[i] = getImpl(Ty->getContainedType(i)); |
| 275 | AnyChange |= ElementTypes[i] != Ty->getContainedType(i); |
| 276 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 277 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 278 | // If we found our type while recursively processing stuff, just use it. |
| 279 | Entry = &MappedTypes[Ty]; |
| 280 | if (*Entry) return *Entry; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 281 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 282 | // If all of the element types mapped directly over, then the type is usable |
| 283 | // as-is. |
| 284 | if (!AnyChange) |
| 285 | return *Entry = Ty; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 286 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 287 | // Otherwise, rebuild a modified type. |
| 288 | switch (Ty->getTypeID()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 289 | default: llvm_unreachable("unknown derived type to remap"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 290 | case Type::ArrayTyID: |
| 291 | return *Entry = ArrayType::get(ElementTypes[0], |
| 292 | cast<ArrayType>(Ty)->getNumElements()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 293 | case Type::VectorTyID: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 294 | return *Entry = VectorType::get(ElementTypes[0], |
| 295 | cast<VectorType>(Ty)->getNumElements()); |
| 296 | case Type::PointerTyID: |
| 297 | return *Entry = PointerType::get(ElementTypes[0], |
| 298 | cast<PointerType>(Ty)->getAddressSpace()); |
| 299 | case Type::FunctionTyID: |
| 300 | return *Entry = FunctionType::get(ElementTypes[0], |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 301 | makeArrayRef(ElementTypes).slice(1), |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 302 | cast<FunctionType>(Ty)->isVarArg()); |
| 303 | case Type::StructTyID: |
| 304 | // Note that this is only reached for anonymous structs. |
| 305 | return *Entry = StructType::get(Ty->getContext(), ElementTypes, |
| 306 | cast<StructType>(Ty)->isPacked()); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // Otherwise, this is an unmapped named struct. If the struct can be directly |
| 311 | // mapped over, just use it as-is. This happens in a case when the linked-in |
| 312 | // module has something like: |
| 313 | // %T = type {%T*, i32} |
| 314 | // @GV = global %T* null |
| 315 | // where T does not exist at all in the destination module. |
| 316 | // |
| 317 | // The other case we watch for is when the type is not in the destination |
| 318 | // module, but that it has to be rebuilt because it refers to something that |
| 319 | // is already mapped. For example, if the destination module has: |
| 320 | // %A = type { i32 } |
| 321 | // and the source module has something like |
| 322 | // %A' = type { i32 } |
| 323 | // %B = type { %A'* } |
| 324 | // @GV = global %B* null |
| 325 | // then we want to create a new type: "%B = type { %A*}" and have it take the |
| 326 | // pristine "%B" name from the source module. |
| 327 | // |
| 328 | // To determine which case this is, we have to recursively walk the type graph |
| 329 | // speculating that we'll be able to reuse it unmodified. Only if this is |
| 330 | // safe would we map the entire thing over. Because this is an optimization, |
| 331 | // and is not required for the prettiness of the linked module, we just skip |
| 332 | // it and always rebuild a type here. |
| 333 | StructType *STy = cast<StructType>(Ty); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 334 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 335 | // If the type is opaque, we can just use it directly. |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 336 | if (STy->isOpaque()) { |
| 337 | // A named structure type from src module is used. Add it to the Set of |
| 338 | // identified structs in the destination module. |
| 339 | DstStructTypesSet.insert(STy); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 340 | return *Entry = STy; |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 341 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 342 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 343 | // Otherwise we create a new type and resolve its body later. This will be |
| 344 | // resolved by the top level of get(). |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 345 | SrcDefinitionsToResolve.push_back(STy); |
| 346 | StructType *DTy = StructType::create(STy->getContext()); |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 347 | // A new identified structure type was created. Add it to the set of |
| 348 | // identified structs in the destination module. |
| 349 | DstStructTypesSet.insert(DTy); |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 350 | DstResolvedOpaqueTypes.insert(DTy); |
| 351 | return *Entry = DTy; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 354 | //===----------------------------------------------------------------------===// |
| 355 | // ModuleLinker implementation. |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | |
| 358 | namespace { |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 359 | class ModuleLinker; |
| 360 | |
| 361 | /// ValueMaterializerTy - Creates prototypes for functions that are lazily |
| 362 | /// linked on the fly. This speeds up linking for modules with many |
| 363 | /// lazily linked functions of which few get used. |
| 364 | class ValueMaterializerTy : public ValueMaterializer { |
| 365 | TypeMapTy &TypeMap; |
| 366 | Module *DstM; |
| 367 | std::vector<Function*> &LazilyLinkFunctions; |
| 368 | public: |
| 369 | ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM, |
| 370 | std::vector<Function*> &LazilyLinkFunctions) : |
| 371 | ValueMaterializer(), TypeMap(TypeMap), DstM(DstM), |
| 372 | LazilyLinkFunctions(LazilyLinkFunctions) { |
| 373 | } |
| 374 | |
Craig Topper | 8548299 | 2014-03-05 07:52:44 +0000 | [diff] [blame] | 375 | Value *materializeValueFor(Value *V) override; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 376 | }; |
| 377 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 378 | /// ModuleLinker - This is an implementation class for the LinkModules |
| 379 | /// function, which is the entrypoint for this file. |
| 380 | class ModuleLinker { |
| 381 | Module *DstM, *SrcM; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 382 | |
| 383 | TypeMapTy TypeMap; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 384 | ValueMaterializerTy ValMaterializer; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 385 | |
| 386 | /// ValueMap - Mapping of values from what they used to be in Src, to what |
| 387 | /// they are now in DstM. ValueToValueMapTy is a ValueMap, which involves |
| 388 | /// some overhead due to the use of Value handles which the Linker doesn't |
| 389 | /// actually need, but this allows us to reuse the ValueMapper code. |
| 390 | ValueToValueMapTy ValueMap; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 391 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 392 | struct AppendingVarInfo { |
| 393 | GlobalVariable *NewGV; // New aggregate global in dest module. |
| 394 | Constant *DstInit; // Old initializer from dest module. |
| 395 | Constant *SrcInit; // Old initializer from src module. |
| 396 | }; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 397 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 398 | std::vector<AppendingVarInfo> AppendingVars; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 399 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 400 | unsigned Mode; // Mode to treat source module. |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 401 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 402 | // Set of items not to link in from source. |
| 403 | SmallPtrSet<const Value*, 16> DoNotLinkFromSource; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 404 | |
Tanya Lattner | 0a48b87 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 405 | // Vector of functions to lazily link in. |
Bill Wendling | fa228782 | 2013-03-27 17:54:41 +0000 | [diff] [blame] | 406 | std::vector<Function*> LazilyLinkFunctions; |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 407 | |
| 408 | bool SuppressWarnings; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 409 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 410 | public: |
| 411 | std::string ErrorMsg; |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 412 | |
| 413 | ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM, unsigned mode, |
| 414 | bool SuppressWarnings=false) |
| 415 | : DstM(dstM), SrcM(srcM), TypeMap(Set), |
| 416 | ValMaterializer(TypeMap, DstM, LazilyLinkFunctions), Mode(mode), |
| 417 | SuppressWarnings(SuppressWarnings) {} |
| 418 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 419 | bool run(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 420 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 421 | private: |
| 422 | /// emitError - Helper method for setting a message and returning an error |
| 423 | /// code. |
| 424 | bool emitError(const Twine &Message) { |
| 425 | ErrorMsg = Message.str(); |
Chris Lattner | 9995302 | 2008-06-16 18:27:53 +0000 | [diff] [blame] | 426 | return true; |
Chris Lattner | 9be1589 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 427 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 428 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 429 | /// getLinkageResult - This analyzes the two global values and determines |
| 430 | /// what the result will look like in the destination module. |
| 431 | bool getLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 432 | GlobalValue::LinkageTypes <, |
| 433 | GlobalValue::VisibilityTypes &Vis, |
| 434 | bool &LinkFromSrc); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 435 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 436 | /// getLinkedToGlobal - Given a global in the source module, return the |
| 437 | /// global in the destination module that is being linked to, if any. |
| 438 | GlobalValue *getLinkedToGlobal(GlobalValue *SrcGV) { |
| 439 | // If the source has no name it can't link. If it has local linkage, |
| 440 | // there is no name match-up going on. |
| 441 | if (!SrcGV->hasName() || SrcGV->hasLocalLinkage()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 442 | return nullptr; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 443 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 444 | // Otherwise see if we have a match in the destination module's symtab. |
| 445 | GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName()); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 446 | if (!DGV) return nullptr; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 447 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 448 | // If we found a global with the same name in the dest module, but it has |
| 449 | // internal linkage, we are really not doing any linkage here. |
| 450 | if (DGV->hasLocalLinkage()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 451 | return nullptr; |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 452 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 453 | // Otherwise, we do in fact link to the destination global. |
| 454 | return DGV; |
| 455 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 456 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 457 | void computeTypeMapping(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 458 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 459 | bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV); |
| 460 | bool linkGlobalProto(GlobalVariable *SrcGV); |
| 461 | bool linkFunctionProto(Function *SrcF); |
| 462 | bool linkAliasProto(GlobalAlias *SrcA); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 463 | bool linkModuleFlagsMetadata(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 464 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 465 | void linkAppendingVarInit(const AppendingVarInfo &AVI); |
| 466 | void linkGlobalInits(); |
| 467 | void linkFunctionBody(Function *Dst, Function *Src); |
| 468 | void linkAliasBodies(); |
| 469 | void linkNamedMDNodes(); |
| 470 | }; |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 473 | /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict |
Reid Spencer | 90246aa | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 474 | /// in the symbol table. This is good for all clients except for us. Go |
| 475 | /// through the trouble to force this back. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 476 | static void forceRenaming(GlobalValue *GV, StringRef Name) { |
| 477 | // If the global doesn't force its name or if it already has the right name, |
| 478 | // there is nothing for us to do. |
| 479 | if (GV->hasLocalLinkage() || GV->getName() == Name) |
| 480 | return; |
| 481 | |
| 482 | Module *M = GV->getParent(); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 483 | |
| 484 | // If there is a conflict, rename the conflict. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 485 | if (GlobalValue *ConflictGV = M->getNamedValue(Name)) { |
Chris Lattner | 2a8d2e0 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 486 | GV->takeName(ConflictGV); |
| 487 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 488 | assert(ConflictGV->getName() != Name && "forceRenaming didn't work"); |
Chris Lattner | 2a8d2e0 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 489 | } else { |
| 490 | GV->setName(Name); // Force the name back |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 491 | } |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 492 | } |
Reid Spencer | 90246aa | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 493 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 494 | /// copyGVAttributes - copy additional attributes (those not needed to construct |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 495 | /// a GlobalValue) from the SrcGV to the DestGV. |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 496 | static void copyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) { |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 497 | // Use the maximum alignment, rather than just copying the alignment of SrcGV. |
Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame^] | 498 | auto *DestGO = dyn_cast<GlobalObject>(DestGV); |
Rafael Espindola | a7d9c69 | 2014-05-06 14:51:36 +0000 | [diff] [blame] | 499 | unsigned Alignment; |
Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame^] | 500 | if (DestGO) |
| 501 | Alignment = std::max(DestGO->getAlignment(), SrcGV->getAlignment()); |
Rafael Espindola | a7d9c69 | 2014-05-06 14:51:36 +0000 | [diff] [blame] | 502 | |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 503 | DestGV->copyAttributesFrom(SrcGV); |
Rafael Espindola | a7d9c69 | 2014-05-06 14:51:36 +0000 | [diff] [blame] | 504 | |
Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame^] | 505 | if (DestGO) |
| 506 | DestGO->setAlignment(Alignment); |
Rafael Espindola | a7d9c69 | 2014-05-06 14:51:36 +0000 | [diff] [blame] | 507 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 508 | forceRenaming(DestGV, SrcGV->getName()); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 511 | static bool isLessConstraining(GlobalValue::VisibilityTypes a, |
| 512 | GlobalValue::VisibilityTypes b) { |
| 513 | if (a == GlobalValue::HiddenVisibility) |
| 514 | return false; |
| 515 | if (b == GlobalValue::HiddenVisibility) |
| 516 | return true; |
| 517 | if (a == GlobalValue::ProtectedVisibility) |
| 518 | return false; |
| 519 | if (b == GlobalValue::ProtectedVisibility) |
| 520 | return true; |
| 521 | return false; |
| 522 | } |
| 523 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 524 | Value *ValueMaterializerTy::materializeValueFor(Value *V) { |
| 525 | Function *SF = dyn_cast<Function>(V); |
| 526 | if (!SF) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 527 | return nullptr; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 528 | |
| 529 | Function *DF = Function::Create(TypeMap.get(SF->getFunctionType()), |
| 530 | SF->getLinkage(), SF->getName(), DstM); |
| 531 | copyGVAttributes(DF, SF); |
| 532 | |
| 533 | LazilyLinkFunctions.push_back(SF); |
| 534 | return DF; |
| 535 | } |
| 536 | |
| 537 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 538 | /// getLinkageResult - This analyzes the two global values and determines what |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 539 | /// the result will look like in the destination module. In particular, it |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 540 | /// computes the resultant linkage type and visibility, computes whether the |
| 541 | /// global in the source should be copied over to the destination (replacing |
| 542 | /// the existing one), and computes whether this linkage is an error or not. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 543 | bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 544 | GlobalValue::LinkageTypes <, |
| 545 | GlobalValue::VisibilityTypes &Vis, |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 546 | bool &LinkFromSrc) { |
| 547 | assert(Dest && "Must have two globals being queried"); |
| 548 | assert(!Src->hasLocalLinkage() && |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 549 | "If Src has internal linkage, Dest shouldn't be set!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 550 | |
Peter Collingbourne | 8bb15d8 | 2011-10-30 17:46:34 +0000 | [diff] [blame] | 551 | bool SrcIsDeclaration = Src->isDeclaration() && !Src->isMaterializable(); |
Chris Lattner | 0c134b5 | 2011-07-14 20:23:05 +0000 | [diff] [blame] | 552 | bool DestIsDeclaration = Dest->isDeclaration(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 553 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 554 | if (SrcIsDeclaration) { |
Anton Korobeynikov | 1f93c50 | 2008-03-10 22:33:22 +0000 | [diff] [blame] | 555 | // If Src is external or if both Src & Dest are external.. Just link the |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 556 | // external globals, we aren't adding anything. |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 557 | if (Src->hasDLLImportStorageClass()) { |
| 558 | // If one of GVs is marked as DLLImport, result should be dllimport'ed. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 559 | if (DestIsDeclaration) { |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 560 | LinkFromSrc = true; |
| 561 | LT = Src->getLinkage(); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 562 | } |
Andrew Lenharth | e06036d | 2006-12-15 17:35:32 +0000 | [diff] [blame] | 563 | } else if (Dest->hasExternalWeakLinkage()) { |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 564 | // If the Dest is weak, use the source linkage. |
Andrew Lenharth | e06036d | 2006-12-15 17:35:32 +0000 | [diff] [blame] | 565 | LinkFromSrc = true; |
| 566 | LT = Src->getLinkage(); |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 567 | } else { |
| 568 | LinkFromSrc = false; |
| 569 | LT = Dest->getLinkage(); |
| 570 | } |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 571 | } else if (DestIsDeclaration && !Dest->hasDLLImportStorageClass()) { |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 572 | // If Dest is external but Src is not: |
| 573 | LinkFromSrc = true; |
| 574 | LT = Src->getLinkage(); |
Duncan Sands | d725c99 | 2009-03-08 13:35:23 +0000 | [diff] [blame] | 575 | } else if (Src->isWeakForLinker()) { |
Dale Johannesen | ce4396b | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 576 | // At this point we know that Dest has LinkOnce, External*, Weak, Common, |
| 577 | // or DLL* linkage. |
Chris Lattner | 184f1be | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 578 | if (Dest->hasExternalWeakLinkage() || |
| 579 | Dest->hasAvailableExternallyLinkage() || |
| 580 | (Dest->hasLinkOnceLinkage() && |
| 581 | (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) { |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 582 | LinkFromSrc = true; |
| 583 | LT = Src->getLinkage(); |
| 584 | } else { |
| 585 | LinkFromSrc = false; |
| 586 | LT = Dest->getLinkage(); |
| 587 | } |
Duncan Sands | d725c99 | 2009-03-08 13:35:23 +0000 | [diff] [blame] | 588 | } else if (Dest->isWeakForLinker()) { |
Anton Korobeynikov | 12c9494 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 589 | // At this point we know that Src has External* or DLL* linkage. |
| 590 | if (Src->hasExternalWeakLinkage()) { |
| 591 | LinkFromSrc = false; |
| 592 | LT = Dest->getLinkage(); |
| 593 | } else { |
| 594 | LinkFromSrc = true; |
| 595 | LT = GlobalValue::ExternalLinkage; |
| 596 | } |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 597 | } else { |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 598 | assert((Dest->hasExternalLinkage() || Dest->hasExternalWeakLinkage()) && |
| 599 | (Src->hasExternalLinkage() || Src->hasExternalWeakLinkage()) && |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 600 | "Unexpected linkage type!"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 601 | return emitError("Linking globals named '" + Src->getName() + |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 602 | "': symbol multiply defined!"); |
| 603 | } |
Anton Korobeynikov | 31fc4f9 | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 604 | |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 605 | // Compute the visibility. We follow the rules in the System V Application |
| 606 | // Binary Interface. |
Duncan P. N. Exon Smith | b2becfd | 2014-05-07 22:55:46 +0000 | [diff] [blame] | 607 | assert(!GlobalValue::isLocalLinkage(LT) && |
| 608 | "Symbols with local linkage should not be merged"); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 609 | Vis = isLessConstraining(Src->getVisibility(), Dest->getVisibility()) ? |
| 610 | Dest->getVisibility() : Src->getVisibility(); |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 611 | return false; |
| 612 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 613 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 614 | /// computeTypeMapping - Loop over all of the linked values to compute type |
| 615 | /// mappings. For example, if we link "extern Foo *x" and "Foo *x = NULL", then |
| 616 | /// we have two struct types 'Foo' but one got renamed when the module was |
| 617 | /// loaded into the same LLVMContext. |
| 618 | void ModuleLinker::computeTypeMapping() { |
| 619 | // Incorporate globals. |
| 620 | for (Module::global_iterator I = SrcM->global_begin(), |
| 621 | E = SrcM->global_end(); I != E; ++I) { |
| 622 | GlobalValue *DGV = getLinkedToGlobal(I); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 623 | if (!DGV) continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 624 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 625 | if (!DGV->hasAppendingLinkage() || !I->hasAppendingLinkage()) { |
| 626 | TypeMap.addTypeMapping(DGV->getType(), I->getType()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 627 | continue; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 628 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 629 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 630 | // Unify the element type of appending arrays. |
| 631 | ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType()); |
| 632 | ArrayType *SAT = cast<ArrayType>(I->getType()->getElementType()); |
| 633 | TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType()); |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 634 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 635 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 636 | // Incorporate functions. |
| 637 | for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) { |
| 638 | if (GlobalValue *DGV = getLinkedToGlobal(I)) |
| 639 | TypeMap.addTypeMapping(DGV->getType(), I->getType()); |
| 640 | } |
Bill Wendling | 7b46461 | 2012-02-27 22:34:19 +0000 | [diff] [blame] | 641 | |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 642 | // Incorporate types by name, scanning all the types in the source module. |
| 643 | // At this point, the destination module may have a type "%foo = { i32 }" for |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 644 | // example. When the source module got loaded into the same LLVMContext, if |
| 645 | // it had the same type, it would have been renamed to "%foo.42 = { i32 }". |
Bill Wendling | 8555a37 | 2012-08-03 00:30:35 +0000 | [diff] [blame] | 646 | TypeFinder SrcStructTypes; |
| 647 | SrcStructTypes.run(*SrcM, true); |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 648 | SmallPtrSet<StructType*, 32> SrcStructTypesSet(SrcStructTypes.begin(), |
| 649 | SrcStructTypes.end()); |
Bill Wendling | 8737480 | 2012-03-23 23:17:38 +0000 | [diff] [blame] | 650 | |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 651 | for (unsigned i = 0, e = SrcStructTypes.size(); i != e; ++i) { |
| 652 | StructType *ST = SrcStructTypes[i]; |
| 653 | if (!ST->hasName()) continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 654 | |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 655 | // Check to see if there is a dot in the name followed by a digit. |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 656 | size_t DotPos = ST->getName().rfind('.'); |
| 657 | if (DotPos == 0 || DotPos == StringRef::npos || |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 658 | ST->getName().back() == '.' || |
| 659 | !isdigit(static_cast<unsigned char>(ST->getName()[DotPos+1]))) |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 660 | continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 661 | |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 662 | // Check to see if the destination module has a struct with the prefix name. |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 663 | if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos))) |
Bill Wendling | 8737480 | 2012-03-23 23:17:38 +0000 | [diff] [blame] | 664 | // Don't use it if this actually came from the source module. They're in |
| 665 | // the same LLVMContext after all. Also don't use it unless the type is |
| 666 | // actually used in the destination module. This can happen in situations |
| 667 | // like this: |
| 668 | // |
| 669 | // Module A Module B |
| 670 | // -------- -------- |
| 671 | // %Z = type { %A } %B = type { %C.1 } |
| 672 | // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* } |
| 673 | // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] } |
| 674 | // %C = type { i8* } %B.3 = type { %C.1 } |
| 675 | // |
| 676 | // When we link Module B with Module A, the '%B' in Module B is |
| 677 | // used. However, that would then use '%C.1'. But when we process '%C.1', |
| 678 | // we prefer to take the '%C' version. So we are then left with both |
| 679 | // '%C.1' and '%C' being used for the same types. This leads to some |
| 680 | // variables using one type and some using the other. |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 681 | if (!SrcStructTypesSet.count(DST) && TypeMap.DstStructTypesSet.count(DST)) |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 682 | TypeMap.addTypeMapping(DST, ST); |
| 683 | } |
| 684 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 685 | // Don't bother incorporating aliases, they aren't generally typed well. |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 686 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 687 | // Now that we have discovered all of the type equivalences, get a body for |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 688 | // any 'opaque' types in the dest module that are now resolved. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 689 | TypeMap.linkDefinedTypeBodies(); |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 692 | /// linkAppendingVarProto - If there were any appending global variables, link |
| 693 | /// them together now. Return true on error. |
| 694 | bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV, |
| 695 | GlobalVariable *SrcGV) { |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 696 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 697 | if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) |
| 698 | return emitError("Linking globals named '" + SrcGV->getName() + |
| 699 | "': can only link appending global with another appending global!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 700 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 701 | ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType()); |
| 702 | ArrayType *SrcTy = |
| 703 | cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType())); |
| 704 | Type *EltTy = DstTy->getElementType(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 705 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 706 | // Check to see that they two arrays agree on type. |
| 707 | if (EltTy != SrcTy->getElementType()) |
| 708 | return emitError("Appending variables with different element types!"); |
| 709 | if (DstGV->isConstant() != SrcGV->isConstant()) |
| 710 | return emitError("Appending variables linked with different const'ness!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 711 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 712 | if (DstGV->getAlignment() != SrcGV->getAlignment()) |
| 713 | return emitError( |
| 714 | "Appending variables with different alignment need to be linked!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 715 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 716 | if (DstGV->getVisibility() != SrcGV->getVisibility()) |
| 717 | return emitError( |
| 718 | "Appending variables with different visibility need to be linked!"); |
Rafael Espindola | fac3a01 | 2013-09-04 15:33:34 +0000 | [diff] [blame] | 719 | |
| 720 | if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr()) |
| 721 | return emitError( |
| 722 | "Appending variables with different unnamed_addr need to be linked!"); |
| 723 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 724 | if (DstGV->getSection() != SrcGV->getSection()) |
| 725 | return emitError( |
| 726 | "Appending variables with different section name need to be linked!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 727 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 728 | uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements(); |
| 729 | ArrayType *NewType = ArrayType::get(EltTy, NewSize); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 730 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 731 | // Create the new global variable. |
| 732 | GlobalVariable *NG = |
| 733 | new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 734 | DstGV->getLinkage(), /*init*/nullptr, /*name*/"", DstGV, |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 735 | DstGV->getThreadLocalMode(), |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 736 | DstGV->getType()->getAddressSpace()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 737 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 738 | // Propagate alignment, visibility and section info. |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 739 | copyGVAttributes(NG, DstGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 740 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 741 | AppendingVarInfo AVI; |
| 742 | AVI.NewGV = NG; |
| 743 | AVI.DstInit = DstGV->getInitializer(); |
| 744 | AVI.SrcInit = SrcGV->getInitializer(); |
| 745 | AppendingVars.push_back(AVI); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 746 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 747 | // Replace any uses of the two global variables with uses of the new |
| 748 | // global. |
| 749 | ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType())); |
Anton Korobeynikov | e79f4c7 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 750 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 751 | DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType())); |
| 752 | DstGV->eraseFromParent(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 753 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 754 | // Track the source variable so we don't try to link it. |
| 755 | DoNotLinkFromSource.insert(SrcGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 756 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 757 | return false; |
| 758 | } |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 759 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 760 | /// linkGlobalProto - Loop through the global variables in the src module and |
| 761 | /// merge them into the dest module. |
| 762 | bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) { |
| 763 | GlobalValue *DGV = getLinkedToGlobal(SGV); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 764 | llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility; |
Rafael Espindola | d4885da | 2013-09-04 14:05:09 +0000 | [diff] [blame] | 765 | bool HasUnnamedAddr = SGV->hasUnnamedAddr(); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 766 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 767 | if (DGV) { |
| 768 | // Concatenation of appending linkage variables is magic and handled later. |
| 769 | if (DGV->hasAppendingLinkage() || SGV->hasAppendingLinkage()) |
| 770 | return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 771 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 772 | // Determine whether linkage of these two globals follows the source |
| 773 | // module's definition or the destination module's definition. |
Chris Lattner | 1b9633d | 2006-11-09 05:18:12 +0000 | [diff] [blame] | 774 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 775 | GlobalValue::VisibilityTypes NV; |
Chris Lattner | 1b9633d | 2006-11-09 05:18:12 +0000 | [diff] [blame] | 776 | bool LinkFromSrc = false; |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 777 | if (getLinkageResult(DGV, SGV, NewLinkage, NV, LinkFromSrc)) |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 778 | return true; |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 779 | NewVisibility = NV; |
Rafael Espindola | fd9a941 | 2013-09-04 14:59:03 +0000 | [diff] [blame] | 780 | HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr(); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 781 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 782 | // If we're not linking from the source, then keep the definition that we |
| 783 | // have. |
| 784 | if (!LinkFromSrc) { |
| 785 | // Special case for const propagation. |
| 786 | if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) |
| 787 | if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant()) |
| 788 | DGVar->setConstant(true); |
Rafael Espindola | d4885da | 2013-09-04 14:05:09 +0000 | [diff] [blame] | 789 | |
| 790 | // Set calculated linkage, visibility and unnamed_addr. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 791 | DGV->setLinkage(NewLinkage); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 792 | DGV->setVisibility(*NewVisibility); |
Rafael Espindola | d4885da | 2013-09-04 14:05:09 +0000 | [diff] [blame] | 793 | DGV->setUnnamedAddr(HasUnnamedAddr); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 794 | |
Chris Lattner | 0ead7a5 | 2008-07-14 07:23:24 +0000 | [diff] [blame] | 795 | // Make sure to remember this mapping. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 796 | ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType())); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 797 | |
| 798 | // Track the source global so that we don't attempt to copy it over when |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 799 | // processing global initializers. |
| 800 | DoNotLinkFromSource.insert(SGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 801 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 802 | return false; |
Chris Lattner | 0ead7a5 | 2008-07-14 07:23:24 +0000 | [diff] [blame] | 803 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 804 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 805 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 806 | // No linking to be performed or linking from the source: simply create an |
| 807 | // identical version of the symbol over in the dest module... the |
| 808 | // initializer will be filled in later by LinkGlobalInits. |
| 809 | GlobalVariable *NewDGV = |
| 810 | new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 811 | SGV->isConstant(), SGV->getLinkage(), /*init*/nullptr, |
| 812 | SGV->getName(), /*insertbefore*/nullptr, |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 813 | SGV->getThreadLocalMode(), |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 814 | SGV->getType()->getAddressSpace()); |
| 815 | // Propagate alignment, visibility and section info. |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 816 | copyGVAttributes(NewDGV, SGV); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 817 | if (NewVisibility) |
| 818 | NewDGV->setVisibility(*NewVisibility); |
Rafael Espindola | d4885da | 2013-09-04 14:05:09 +0000 | [diff] [blame] | 819 | NewDGV->setUnnamedAddr(HasUnnamedAddr); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 820 | |
| 821 | if (DGV) { |
| 822 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType())); |
| 823 | DGV->eraseFromParent(); |
| 824 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 825 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 826 | // Make sure to remember this mapping. |
| 827 | ValueMap[SGV] = NewDGV; |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 831 | /// linkFunctionProto - Link the function in the source module into the |
| 832 | /// destination module if needed, setting up mapping information. |
| 833 | bool ModuleLinker::linkFunctionProto(Function *SF) { |
| 834 | GlobalValue *DGV = getLinkedToGlobal(SF); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 835 | llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility; |
Rafael Espindola | fd9a941 | 2013-09-04 14:59:03 +0000 | [diff] [blame] | 836 | bool HasUnnamedAddr = SF->hasUnnamedAddr(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 837 | |
| 838 | if (DGV) { |
| 839 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 840 | bool LinkFromSrc = false; |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 841 | GlobalValue::VisibilityTypes NV; |
| 842 | if (getLinkageResult(DGV, SF, NewLinkage, NV, LinkFromSrc)) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 843 | return true; |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 844 | NewVisibility = NV; |
Rafael Espindola | fd9a941 | 2013-09-04 14:59:03 +0000 | [diff] [blame] | 845 | HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr(); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 846 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 847 | if (!LinkFromSrc) { |
| 848 | // Set calculated linkage |
| 849 | DGV->setLinkage(NewLinkage); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 850 | DGV->setVisibility(*NewVisibility); |
Rafael Espindola | fd9a941 | 2013-09-04 14:59:03 +0000 | [diff] [blame] | 851 | DGV->setUnnamedAddr(HasUnnamedAddr); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 852 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 853 | // Make sure to remember this mapping. |
| 854 | ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType())); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 855 | |
| 856 | // Track the function from the source module so we don't attempt to remap |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 857 | // it. |
| 858 | DoNotLinkFromSource.insert(SF); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 859 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 860 | return false; |
| 861 | } |
Anton Korobeynikov | dac5fa9 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 862 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 863 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 864 | // If the function is to be lazily linked, don't create it just yet. |
| 865 | // The ValueMaterializerTy will deal with creating it if it's used. |
| 866 | if (!DGV && (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() || |
| 867 | SF->hasAvailableExternallyLinkage())) { |
| 868 | DoNotLinkFromSource.insert(SF); |
| 869 | return false; |
| 870 | } |
| 871 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 872 | // If there is no linkage to be performed or we are linking from the source, |
| 873 | // bring SF over. |
| 874 | Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()), |
| 875 | SF->getLinkage(), SF->getName(), DstM); |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 876 | copyGVAttributes(NewDF, SF); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 877 | if (NewVisibility) |
| 878 | NewDF->setVisibility(*NewVisibility); |
Rafael Espindola | fd9a941 | 2013-09-04 14:59:03 +0000 | [diff] [blame] | 879 | NewDF->setUnnamedAddr(HasUnnamedAddr); |
Anton Korobeynikov | dac5fa9 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 880 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 881 | if (DGV) { |
| 882 | // Any uses of DF need to change to NewDF, with cast. |
| 883 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType())); |
| 884 | DGV->eraseFromParent(); |
Lauro Ramos Venancio | b00c9c0 | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 885 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 886 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 887 | ValueMap[SF] = NewDF; |
Lauro Ramos Venancio | b00c9c0 | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 888 | return false; |
| 889 | } |
| 890 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 891 | /// LinkAliasProto - Set up prototypes for any aliases that come over from the |
| 892 | /// source module. |
| 893 | bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) { |
| 894 | GlobalValue *DGV = getLinkedToGlobal(SGA); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 895 | llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility; |
| 896 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 897 | if (DGV) { |
| 898 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 899 | GlobalValue::VisibilityTypes NV; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 900 | bool LinkFromSrc = false; |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 901 | if (getLinkageResult(DGV, SGA, NewLinkage, NV, LinkFromSrc)) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 902 | return true; |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 903 | NewVisibility = NV; |
| 904 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 905 | if (!LinkFromSrc) { |
| 906 | // Set calculated linkage. |
| 907 | DGV->setLinkage(NewLinkage); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 908 | DGV->setVisibility(*NewVisibility); |
| 909 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 910 | // Make sure to remember this mapping. |
| 911 | ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType())); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 912 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 913 | // Track the alias from the source module so we don't attempt to remap it. |
| 914 | DoNotLinkFromSource.insert(SGA); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 915 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 916 | return false; |
| 917 | } |
| 918 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 919 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 920 | // If there is no linkage to be performed or we're linking from the source, |
| 921 | // bring over SGA. |
| 922 | GlobalAlias *NewDA = new GlobalAlias(TypeMap.get(SGA->getType()), |
| 923 | SGA->getLinkage(), SGA->getName(), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 924 | /*aliasee*/nullptr, DstM); |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 925 | copyGVAttributes(NewDA, SGA); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 926 | if (NewVisibility) |
| 927 | NewDA->setVisibility(*NewVisibility); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 928 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 929 | if (DGV) { |
| 930 | // Any uses of DGV need to change to NewDA, with cast. |
| 931 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType())); |
| 932 | DGV->eraseFromParent(); |
| 933 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 934 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 935 | ValueMap[SGA] = NewDA; |
| 936 | return false; |
| 937 | } |
| 938 | |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 939 | static void getArrayElements(Constant *C, SmallVectorImpl<Constant*> &Dest) { |
Chris Lattner | 6705883 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 940 | unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements(); |
| 941 | |
| 942 | for (unsigned i = 0; i != NumElements; ++i) |
| 943 | Dest.push_back(C->getAggregateElement(i)); |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 944 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 945 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 946 | void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) { |
| 947 | // Merge the initializer. |
| 948 | SmallVector<Constant*, 16> Elements; |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 949 | getArrayElements(AVI.DstInit, Elements); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 950 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 951 | Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap, &ValMaterializer); |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 952 | getArrayElements(SrcInit, Elements); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 953 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 954 | ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType()); |
| 955 | AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements)); |
| 956 | } |
| 957 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 958 | /// linkGlobalInits - Update the initializers in the Dest module now that all |
| 959 | /// globals that may be referenced are in Dest. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 960 | void ModuleLinker::linkGlobalInits() { |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 961 | // Loop over all of the globals in the src module, mapping them over as we go |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 962 | for (Module::const_global_iterator I = SrcM->global_begin(), |
| 963 | E = SrcM->global_end(); I != E; ++I) { |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 964 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 965 | // Only process initialized GV's or ones not already in dest. |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 966 | if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue; |
| 967 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 968 | // Grab destination global variable. |
| 969 | GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]); |
| 970 | // Figure out what the initializer looks like in the dest module. |
| 971 | DGV->setInitializer(MapValue(I->getInitializer(), ValueMap, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 972 | RF_None, &TypeMap, &ValMaterializer)); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 973 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 976 | /// linkFunctionBody - Copy the source function over into the dest function and |
| 977 | /// fix up references to values. At this point we know that Dest is an external |
| 978 | /// function, and that Src is not. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 979 | void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) { |
| 980 | assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration()); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 981 | |
Chris Lattner | 7391dde | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 982 | // Go through and convert function arguments over, remembering the mapping. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 983 | Function::arg_iterator DI = Dst->arg_begin(); |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 984 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 985 | I != E; ++I, ++DI) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 986 | DI->setName(I->getName()); // Copy the name over. |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 987 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 988 | // Add a mapping to our mapping. |
Anton Korobeynikov | 66a6271 | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 989 | ValueMap[I] = DI; |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 992 | if (Mode == Linker::DestroySource) { |
| 993 | // Splice the body of the source function into the dest function. |
| 994 | Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 995 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 996 | // At this point, all of the instructions and values of the function are now |
| 997 | // copied over. The only problem is that they are still referencing values in |
| 998 | // the Source function as operands. Loop through all of the operands of the |
| 999 | // functions and patch them up to point to the local versions. |
| 1000 | for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB) |
| 1001 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1002 | RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, |
| 1003 | &TypeMap, &ValMaterializer); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1004 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1005 | } else { |
| 1006 | // Clone the body of the function into the dest function. |
| 1007 | SmallVector<ReturnInst*, 8> Returns; // Ignore returns. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1008 | CloneFunctionInto(Dst, Src, ValueMap, false, Returns, "", nullptr, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1009 | &TypeMap, &ValMaterializer); |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1010 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1011 | |
Chris Lattner | 7391dde | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 1012 | // There is no need to map the arguments anymore. |
Chris Lattner | 44ab8ae | 2006-06-16 01:24:04 +0000 | [diff] [blame] | 1013 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
| 1014 | I != E; ++I) |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1015 | ValueMap.erase(I); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1016 | |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1019 | /// linkAliasBodies - Insert all of the aliases in Src into the Dest module. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1020 | void ModuleLinker::linkAliasBodies() { |
| 1021 | for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end(); |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1022 | I != E; ++I) { |
| 1023 | if (DoNotLinkFromSource.count(I)) |
| 1024 | continue; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1025 | if (Constant *Aliasee = I->getAliasee()) { |
| 1026 | GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]); |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1027 | DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None, |
| 1028 | &TypeMap, &ValMaterializer)); |
David Chisnall | 2c4a34a | 2010-01-09 16:27:31 +0000 | [diff] [blame] | 1029 | } |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1030 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1031 | } |
Anton Korobeynikov | 2609888 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1032 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1033 | /// linkNamedMDNodes - Insert all of the named MDNodes in Src into the Dest |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1034 | /// module. |
| 1035 | void ModuleLinker::linkNamedMDNodes() { |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1036 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1037 | for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(), |
| 1038 | E = SrcM->named_metadata_end(); I != E; ++I) { |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1039 | // Don't link module flags here. Do them separately. |
| 1040 | if (&*I == SrcModFlags) continue; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1041 | NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName()); |
| 1042 | // Add Src elements into Dest node. |
| 1043 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 1044 | DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1045 | RF_None, &TypeMap, &ValMaterializer)); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1048 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1049 | /// linkModuleFlagsMetadata - Merge the linker flags in Src into the Dest |
| 1050 | /// module. |
| 1051 | bool ModuleLinker::linkModuleFlagsMetadata() { |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1052 | // If the source module has no module flags, we are done. |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1053 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
| 1054 | if (!SrcModFlags) return false; |
| 1055 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1056 | // If the destination module doesn't have module flags yet, then just copy |
| 1057 | // over the source module's flags. |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1058 | NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata(); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1059 | if (DstModFlags->getNumOperands() == 0) { |
| 1060 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) |
| 1061 | DstModFlags->addOperand(SrcModFlags->getOperand(I)); |
| 1062 | |
| 1063 | return false; |
| 1064 | } |
| 1065 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1066 | // First build a map of the existing module flags and requirements. |
| 1067 | DenseMap<MDString*, MDNode*> Flags; |
| 1068 | SmallSetVector<MDNode*, 16> Requirements; |
| 1069 | for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) { |
| 1070 | MDNode *Op = DstModFlags->getOperand(I); |
| 1071 | ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0)); |
| 1072 | MDString *ID = cast<MDString>(Op->getOperand(1)); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1073 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1074 | if (Behavior->getZExtValue() == Module::Require) { |
| 1075 | Requirements.insert(cast<MDNode>(Op->getOperand(2))); |
| 1076 | } else { |
| 1077 | Flags[ID] = Op; |
| 1078 | } |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1081 | // Merge in the flags from the source module, and also collect its set of |
| 1082 | // requirements. |
| 1083 | bool HasErr = false; |
| 1084 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) { |
| 1085 | MDNode *SrcOp = SrcModFlags->getOperand(I); |
| 1086 | ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0)); |
| 1087 | MDString *ID = cast<MDString>(SrcOp->getOperand(1)); |
| 1088 | MDNode *DstOp = Flags.lookup(ID); |
| 1089 | unsigned SrcBehaviorValue = SrcBehavior->getZExtValue(); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1090 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1091 | // If this is a requirement, add it and continue. |
| 1092 | if (SrcBehaviorValue == Module::Require) { |
| 1093 | // If the destination module does not already have this requirement, add |
| 1094 | // it. |
| 1095 | if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) { |
| 1096 | DstModFlags->addOperand(SrcOp); |
| 1097 | } |
| 1098 | continue; |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1101 | // If there is no existing flag with this ID, just add it. |
| 1102 | if (!DstOp) { |
| 1103 | Flags[ID] = SrcOp; |
| 1104 | DstModFlags->addOperand(SrcOp); |
| 1105 | continue; |
| 1106 | } |
| 1107 | |
| 1108 | // Otherwise, perform a merge. |
| 1109 | ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0)); |
| 1110 | unsigned DstBehaviorValue = DstBehavior->getZExtValue(); |
| 1111 | |
| 1112 | // If either flag has override behavior, handle it first. |
| 1113 | if (DstBehaviorValue == Module::Override) { |
| 1114 | // Diagnose inconsistent flags which both have override behavior. |
| 1115 | if (SrcBehaviorValue == Module::Override && |
| 1116 | SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
| 1117 | HasErr |= emitError("linking module flags '" + ID->getString() + |
| 1118 | "': IDs have conflicting override values"); |
| 1119 | } |
| 1120 | continue; |
| 1121 | } else if (SrcBehaviorValue == Module::Override) { |
| 1122 | // Update the destination flag to that of the source. |
| 1123 | DstOp->replaceOperandWith(0, SrcBehavior); |
| 1124 | DstOp->replaceOperandWith(2, SrcOp->getOperand(2)); |
| 1125 | continue; |
| 1126 | } |
| 1127 | |
| 1128 | // Diagnose inconsistent merge behavior types. |
| 1129 | if (SrcBehaviorValue != DstBehaviorValue) { |
| 1130 | HasErr |= emitError("linking module flags '" + ID->getString() + |
| 1131 | "': IDs have conflicting behaviors"); |
| 1132 | continue; |
| 1133 | } |
| 1134 | |
| 1135 | // Perform the merge for standard behavior types. |
| 1136 | switch (SrcBehaviorValue) { |
| 1137 | case Module::Require: |
| 1138 | case Module::Override: assert(0 && "not possible"); break; |
| 1139 | case Module::Error: { |
| 1140 | // Emit an error if the values differ. |
| 1141 | if (SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
| 1142 | HasErr |= emitError("linking module flags '" + ID->getString() + |
| 1143 | "': IDs have conflicting values"); |
| 1144 | } |
| 1145 | continue; |
| 1146 | } |
| 1147 | case Module::Warning: { |
| 1148 | // Emit a warning if the values differ. |
| 1149 | if (SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1150 | if (!SuppressWarnings) { |
| 1151 | errs() << "WARNING: linking module flags '" << ID->getString() |
| 1152 | << "': IDs have conflicting values"; |
| 1153 | } |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1154 | } |
| 1155 | continue; |
| 1156 | } |
Daniel Dunbar | d77d9fb | 2013-01-16 21:38:56 +0000 | [diff] [blame] | 1157 | case Module::Append: { |
| 1158 | MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); |
| 1159 | MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); |
| 1160 | unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands(); |
| 1161 | Value **VP, **Values = VP = new Value*[NumOps]; |
| 1162 | for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP) |
| 1163 | *VP = DstValue->getOperand(i); |
| 1164 | for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP) |
| 1165 | *VP = SrcValue->getOperand(i); |
| 1166 | DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(), |
| 1167 | ArrayRef<Value*>(Values, |
| 1168 | NumOps))); |
| 1169 | delete[] Values; |
| 1170 | break; |
| 1171 | } |
| 1172 | case Module::AppendUnique: { |
| 1173 | SmallSetVector<Value*, 16> Elts; |
| 1174 | MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); |
| 1175 | MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); |
| 1176 | for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i) |
| 1177 | Elts.insert(DstValue->getOperand(i)); |
| 1178 | for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i) |
| 1179 | Elts.insert(SrcValue->getOperand(i)); |
| 1180 | DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(), |
| 1181 | ArrayRef<Value*>(Elts.begin(), |
| 1182 | Elts.end()))); |
| 1183 | break; |
| 1184 | } |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1185 | } |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1188 | // Check all of the requirements. |
| 1189 | for (unsigned I = 0, E = Requirements.size(); I != E; ++I) { |
| 1190 | MDNode *Requirement = Requirements[I]; |
| 1191 | MDString *Flag = cast<MDString>(Requirement->getOperand(0)); |
| 1192 | Value *ReqValue = Requirement->getOperand(1); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1193 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1194 | MDNode *Op = Flags[Flag]; |
| 1195 | if (!Op || Op->getOperand(2) != ReqValue) { |
| 1196 | HasErr |= emitError("linking module flags '" + Flag->getString() + |
| 1197 | "': does not have the required value"); |
| 1198 | continue; |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | return HasErr; |
| 1203 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1204 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1205 | bool ModuleLinker::run() { |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1206 | assert(DstM && "Null destination module"); |
| 1207 | assert(SrcM && "Null source module"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1208 | |
| 1209 | // Inherit the target data from the source module if the destination module |
| 1210 | // doesn't have one already. |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 1211 | if (!DstM->getDataLayout() && SrcM->getDataLayout()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1212 | DstM->setDataLayout(SrcM->getDataLayout()); |
| 1213 | |
| 1214 | // Copy the target triple from the source to dest if the dest's is empty. |
| 1215 | if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty()) |
| 1216 | DstM->setTargetTriple(SrcM->getTargetTriple()); |
| 1217 | |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 1218 | if (SrcM->getDataLayout() && DstM->getDataLayout() && |
Rafael Espindola | ae593f1 | 2014-02-26 17:02:08 +0000 | [diff] [blame] | 1219 | *SrcM->getDataLayout() != *DstM->getDataLayout()) { |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1220 | if (!SuppressWarnings) { |
JF Bastien | 026fc5f | 2014-03-05 21:26:42 +0000 | [diff] [blame] | 1221 | errs() << "WARNING: Linking two modules of different data layouts: '" |
| 1222 | << SrcM->getModuleIdentifier() << "' is '" |
| 1223 | << SrcM->getDataLayoutStr() << "' whereas '" |
| 1224 | << DstM->getModuleIdentifier() << "' is '" |
| 1225 | << DstM->getDataLayoutStr() << "'\n"; |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1226 | } |
| 1227 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1228 | if (!SrcM->getTargetTriple().empty() && |
| 1229 | DstM->getTargetTriple() != SrcM->getTargetTriple()) { |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1230 | if (!SuppressWarnings) { |
JF Bastien | 026fc5f | 2014-03-05 21:26:42 +0000 | [diff] [blame] | 1231 | errs() << "WARNING: Linking two modules of different target triples: " |
| 1232 | << SrcM->getModuleIdentifier() << "' is '" |
| 1233 | << SrcM->getTargetTriple() << "' whereas '" |
| 1234 | << DstM->getModuleIdentifier() << "' is '" |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1235 | << DstM->getTargetTriple() << "'\n"; |
| 1236 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | // Append the module inline asm string. |
| 1240 | if (!SrcM->getModuleInlineAsm().empty()) { |
| 1241 | if (DstM->getModuleInlineAsm().empty()) |
| 1242 | DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm()); |
| 1243 | else |
| 1244 | DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+ |
| 1245 | SrcM->getModuleInlineAsm()); |
| 1246 | } |
| 1247 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1248 | // Loop over all of the linked values to compute type mappings. |
| 1249 | computeTypeMapping(); |
| 1250 | |
| 1251 | // Insert all of the globals in src into the DstM module... without linking |
| 1252 | // initializers (which could refer to functions not yet mapped over). |
| 1253 | for (Module::global_iterator I = SrcM->global_begin(), |
| 1254 | E = SrcM->global_end(); I != E; ++I) |
| 1255 | if (linkGlobalProto(I)) |
| 1256 | return true; |
| 1257 | |
| 1258 | // Link the functions together between the two modules, without doing function |
| 1259 | // bodies... this just adds external function prototypes to the DstM |
| 1260 | // function... We do this so that when we begin processing function bodies, |
| 1261 | // all of the global values that may be referenced are available in our |
| 1262 | // ValueMap. |
| 1263 | for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) |
| 1264 | if (linkFunctionProto(I)) |
| 1265 | return true; |
| 1266 | |
| 1267 | // If there were any aliases, link them now. |
| 1268 | for (Module::alias_iterator I = SrcM->alias_begin(), |
| 1269 | E = SrcM->alias_end(); I != E; ++I) |
| 1270 | if (linkAliasProto(I)) |
| 1271 | return true; |
| 1272 | |
| 1273 | for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i) |
| 1274 | linkAppendingVarInit(AppendingVars[i]); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1275 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1276 | // Link in the function bodies that are defined in the source module into |
| 1277 | // DstM. |
| 1278 | for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) { |
Tanya Lattner | ea166d4 | 2011-10-14 22:17:46 +0000 | [diff] [blame] | 1279 | // Skip if not linking from source. |
| 1280 | if (DoNotLinkFromSource.count(SF)) continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1281 | |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1282 | Function *DF = cast<Function>(ValueMap[SF]); |
| 1283 | if (SF->hasPrefixData()) { |
| 1284 | // Link in the prefix data. |
| 1285 | DF->setPrefixData(MapValue( |
| 1286 | SF->getPrefixData(), ValueMap, RF_None, &TypeMap, &ValMaterializer)); |
| 1287 | } |
| 1288 | |
Tanya Lattner | ea166d4 | 2011-10-14 22:17:46 +0000 | [diff] [blame] | 1289 | // Skip if no body (function is external) or materialize. |
| 1290 | if (SF->isDeclaration()) { |
| 1291 | if (!SF->isMaterializable()) |
| 1292 | continue; |
| 1293 | if (SF->Materialize(&ErrorMsg)) |
| 1294 | return true; |
| 1295 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1296 | |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1297 | linkFunctionBody(DF, SF); |
Bill Wendling | 0062378 | 2012-03-23 07:22:49 +0000 | [diff] [blame] | 1298 | SF->Dematerialize(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | // Resolve all uses of aliases with aliasees. |
| 1302 | linkAliasBodies(); |
| 1303 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1304 | // Remap all of the named MDNodes in Src into the DstM module. We do this |
Devang Patel | 6ddbb2e | 2011-08-04 19:44:28 +0000 | [diff] [blame] | 1305 | // after linking GlobalValues so that MDNodes that reference GlobalValues |
| 1306 | // are properly remapped. |
| 1307 | linkNamedMDNodes(); |
| 1308 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1309 | // Merge the module flags into the DstM module. |
| 1310 | if (linkModuleFlagsMetadata()) |
| 1311 | return true; |
| 1312 | |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 1313 | // Update the initializers in the DstM module now that all globals that may |
| 1314 | // be referenced are in DstM. |
| 1315 | linkGlobalInits(); |
| 1316 | |
Tanya Lattner | 0a48b87 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 1317 | // Process vector of lazily linked in functions. |
| 1318 | bool LinkedInAnyFunctions; |
| 1319 | do { |
| 1320 | LinkedInAnyFunctions = false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1321 | |
Bill Wendling | fa228782 | 2013-03-27 17:54:41 +0000 | [diff] [blame] | 1322 | for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(), |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1323 | E = LazilyLinkFunctions.end(); I != E; ++I) { |
Bill Wendling | fa228782 | 2013-03-27 17:54:41 +0000 | [diff] [blame] | 1324 | Function *SF = *I; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1325 | if (!SF) |
| 1326 | continue; |
Bill Wendling | 0062378 | 2012-03-23 07:22:49 +0000 | [diff] [blame] | 1327 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1328 | Function *DF = cast<Function>(ValueMap[SF]); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1329 | if (SF->hasPrefixData()) { |
| 1330 | // Link in the prefix data. |
| 1331 | DF->setPrefixData(MapValue(SF->getPrefixData(), |
| 1332 | ValueMap, |
| 1333 | RF_None, |
| 1334 | &TypeMap, |
| 1335 | &ValMaterializer)); |
| 1336 | } |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Materialize if necessary. |
| 1339 | if (SF->isDeclaration()) { |
| 1340 | if (!SF->isMaterializable()) |
| 1341 | continue; |
| 1342 | if (SF->Materialize(&ErrorMsg)) |
| 1343 | return true; |
Tanya Lattner | 0a48b87 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 1344 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1345 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1346 | // Erase from vector *before* the function body is linked - linkFunctionBody could |
| 1347 | // invalidate I. |
| 1348 | LazilyLinkFunctions.erase(I); |
| 1349 | |
| 1350 | // Link in function body. |
| 1351 | linkFunctionBody(DF, SF); |
| 1352 | SF->Dematerialize(); |
| 1353 | |
| 1354 | // Set flag to indicate we may have more functions to lazily link in |
| 1355 | // since we linked in a function. |
| 1356 | LinkedInAnyFunctions = true; |
| 1357 | break; |
Tanya Lattner | 0a48b87 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 1358 | } |
| 1359 | } while (LinkedInAnyFunctions); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1361 | // Now that all of the types from the source are used, resolve any structs |
| 1362 | // copied over to the dest that didn't exist there. |
| 1363 | TypeMap.linkDefinedTypeBodies(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1364 | |
Anton Korobeynikov | 2609888 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1365 | return false; |
| 1366 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1367 | |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 1368 | Linker::Linker(Module *M, bool SuppressWarnings) |
| 1369 | : Composite(M), SuppressWarnings(SuppressWarnings) { |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 1370 | TypeFinder StructTypes; |
| 1371 | StructTypes.run(*M, true); |
| 1372 | IdentifiedStructTypes.insert(StructTypes.begin(), StructTypes.end()); |
| 1373 | } |
Rafael Espindola | 3df61b7 | 2013-05-04 03:48:37 +0000 | [diff] [blame] | 1374 | |
| 1375 | Linker::~Linker() { |
| 1376 | } |
| 1377 | |
Bill Wendling | 91e6f6e | 2013-10-16 08:59:57 +0000 | [diff] [blame] | 1378 | void Linker::deleteModule() { |
| 1379 | delete Composite; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1380 | Composite = nullptr; |
Bill Wendling | 91e6f6e | 2013-10-16 08:59:57 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Rafael Espindola | 3df61b7 | 2013-05-04 03:48:37 +0000 | [diff] [blame] | 1383 | bool Linker::linkInModule(Module *Src, unsigned Mode, std::string *ErrorMsg) { |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 1384 | ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, Mode, |
| 1385 | SuppressWarnings); |
Rafael Espindola | 287f18b | 2013-05-04 04:08:02 +0000 | [diff] [blame] | 1386 | if (TheLinker.run()) { |
| 1387 | if (ErrorMsg) |
| 1388 | *ErrorMsg = TheLinker.ErrorMsg; |
| 1389 | return true; |
| 1390 | } |
| 1391 | return false; |
Rafael Espindola | 3df61b7 | 2013-05-04 03:48:37 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1394 | //===----------------------------------------------------------------------===// |
| 1395 | // LinkModules entrypoint. |
| 1396 | //===----------------------------------------------------------------------===// |
| 1397 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1398 | /// LinkModules - This function links two modules together, with the resulting |
Eli Bendersky | 970cc63 | 2013-03-08 22:29:44 +0000 | [diff] [blame] | 1399 | /// Dest module modified to be the composite of the two input modules. If an |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1400 | /// error occurs, true is returned and ErrorMsg (if not null) is set to indicate |
| 1401 | /// the problem. Upon failure, the Dest module could be in a modified state, |
| 1402 | /// and shouldn't be relied on to be consistent. |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1403 | bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode, |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1404 | std::string *ErrorMsg) { |
Rafael Espindola | 287f18b | 2013-05-04 04:08:02 +0000 | [diff] [blame] | 1405 | Linker L(Dest); |
| 1406 | return L.linkInModule(Src, Mode, ErrorMsg); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1407 | } |
Bill Wendling | a3aeb98 | 2012-05-09 08:55:40 +0000 | [diff] [blame] | 1408 | |
| 1409 | //===----------------------------------------------------------------------===// |
| 1410 | // C API. |
| 1411 | //===----------------------------------------------------------------------===// |
| 1412 | |
| 1413 | LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src, |
| 1414 | LLVMLinkerMode Mode, char **OutMessages) { |
| 1415 | std::string Messages; |
| 1416 | LLVMBool Result = Linker::LinkModules(unwrap(Dest), unwrap(Src), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1417 | Mode, OutMessages? &Messages : nullptr); |
Bill Wendling | a3aeb98 | 2012-05-09 08:55:40 +0000 | [diff] [blame] | 1418 | if (OutMessages) |
| 1419 | *OutMessages = strdup(Messages.c_str()); |
| 1420 | return Result; |
| 1421 | } |