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" |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SetVector.h" |
Eli Bendersky | 0f7fd36 | 2013-03-19 15:26:24 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Akira Hatanaka | c43df51 | 2015-02-13 00:40:41 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DiagnosticInfo.h" |
| 21 | #include "llvm/IR/DiagnosticPrinter.h" |
| 22 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Chandler Carruth | dcb603f | 2013-01-07 15:43:51 +0000 | [diff] [blame] | 24 | #include "llvm/IR/TypeFinder.h" |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Cloning.h" |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // TypeMap implementation. |
| 30 | //===----------------------------------------------------------------------===// |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 31 | |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 32 | namespace { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 33 | class TypeMapTy : public ValueMapTypeRemapper { |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 34 | /// This is a mapping from a source type to a destination type to use. |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 35 | DenseMap<Type *, Type *> MappedTypes; |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 36 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 37 | /// When checking to see if two subgraphs are isomorphic, we speculatively |
| 38 | /// add types to MappedTypes, but keep track of them here in case we need to |
| 39 | /// roll back. |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 40 | SmallVector<Type *, 16> SpeculativeTypes; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 41 | |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 42 | SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes; |
Rafael Espindola | a96f235 | 2014-11-28 16:41:24 +0000 | [diff] [blame] | 43 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 44 | /// This is a list of non-opaque structs in the source module that are mapped |
| 45 | /// to an opaque struct in the destination module. |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 46 | SmallVector<StructType *, 16> SrcDefinitionsToResolve; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 47 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 48 | /// This is the set of opaque types in the destination modules who are |
| 49 | /// getting a body from the source module. |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 50 | SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes; |
Bill Wendling | 8c2cc41 | 2012-03-22 20:30:41 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 56cdea6 | 2008-06-16 23:06:51 +0000 | [diff] [blame] | 52 | public: |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 53 | TypeMapTy(Linker::IdentifiedStructTypeSet &DstStructTypesSet) |
| 54 | : DstStructTypesSet(DstStructTypesSet) {} |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 55 | |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 56 | Linker::IdentifiedStructTypeSet &DstStructTypesSet; |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 57 | /// Indicate that the specified type in the destination module is conceptually |
| 58 | /// equivalent to the specified type in the source module. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 59 | void addTypeMapping(Type *DstTy, Type *SrcTy); |
| 60 | |
Rafael Espindola | d2a13a2 | 2014-11-25 04:28:31 +0000 | [diff] [blame] | 61 | /// Produce a body for an opaque type in the dest module from a type |
| 62 | /// definition in the source module. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 63 | void linkDefinedTypeBodies(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 64 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 65 | /// Return the mapped type to use for the specified input type from the |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 66 | /// source module. |
| 67 | Type *get(Type *SrcTy); |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 68 | Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited); |
| 69 | |
| 70 | void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 71 | |
Rafael Espindola | 290e2cc | 2014-11-25 14:35:53 +0000 | [diff] [blame] | 72 | FunctionType *get(FunctionType *T) { |
| 73 | return cast<FunctionType>(get((Type *)T)); |
| 74 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 75 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 76 | /// Dump out the type map for debugging purposes. |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 77 | void dump() const { |
Rafael Espindola | 8f14471 | 2014-11-25 06:16:27 +0000 | [diff] [blame] | 78 | for (auto &Pair : MappedTypes) { |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 79 | dbgs() << "TypeMap: "; |
Rafael Espindola | 8f14471 | 2014-11-25 06:16:27 +0000 | [diff] [blame] | 80 | Pair.first->print(dbgs()); |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 81 | dbgs() << " => "; |
Rafael Espindola | 8f14471 | 2014-11-25 06:16:27 +0000 | [diff] [blame] | 82 | Pair.second->print(dbgs()); |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 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: |
Rafael Espindola | 290e2cc | 2014-11-25 14:35:53 +0000 | [diff] [blame] | 88 | Type *remapType(Type *SrcTy) override { return get(SrcTy); } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 89 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 90 | bool areTypesIsomorphic(Type *DstTy, Type *SrcTy); |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 91 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 92 | } |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 93 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 94 | void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) { |
Rafael Espindola | 3d09741 | 2014-11-28 16:26:14 +0000 | [diff] [blame] | 95 | assert(SpeculativeTypes.empty()); |
Rafael Espindola | a96f235 | 2014-11-28 16:41:24 +0000 | [diff] [blame] | 96 | assert(SpeculativeDstOpaqueTypes.empty()); |
Rafael Espindola | 3d09741 | 2014-11-28 16:26:14 +0000 | [diff] [blame] | 97 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 98 | // Check to see if these types are recursively isomorphic and establish a |
| 99 | // mapping between them if so. |
Duncan P. N. Exon Smith | c586eaa | 2014-11-27 17:01:10 +0000 | [diff] [blame] | 100 | if (!areTypesIsomorphic(DstTy, SrcTy)) { |
| 101 | // Oops, they aren't isomorphic. Just discard this request by rolling out |
| 102 | // any speculative mappings we've established. |
Rafael Espindola | 3d09741 | 2014-11-28 16:26:14 +0000 | [diff] [blame] | 103 | for (Type *Ty : SpeculativeTypes) |
| 104 | MappedTypes.erase(Ty); |
Rafael Espindola | a96f235 | 2014-11-28 16:41:24 +0000 | [diff] [blame] | 105 | |
| 106 | SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() - |
| 107 | SpeculativeDstOpaqueTypes.size()); |
| 108 | for (StructType *Ty : SpeculativeDstOpaqueTypes) |
| 109 | DstResolvedOpaqueTypes.erase(Ty); |
Rafael Espindola | 04a74af | 2014-12-01 04:15:59 +0000 | [diff] [blame] | 110 | } else { |
| 111 | for (Type *Ty : SpeculativeTypes) |
| 112 | if (auto *STy = dyn_cast<StructType>(Ty)) |
| 113 | if (STy->hasName()) |
| 114 | STy->setName(""); |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 116 | SpeculativeTypes.clear(); |
Rafael Espindola | a96f235 | 2014-11-28 16:41:24 +0000 | [diff] [blame] | 117 | SpeculativeDstOpaqueTypes.clear(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 118 | } |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 119 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 120 | /// Recursively walk this pair of types, returning true if they are isomorphic, |
| 121 | /// false if they are not. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 122 | bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) { |
| 123 | // Two types with differing kinds are clearly not isomorphic. |
Rafael Espindola | 290e2cc | 2014-11-25 14:35:53 +0000 | [diff] [blame] | 124 | if (DstTy->getTypeID() != SrcTy->getTypeID()) |
| 125 | return false; |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 126 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 127 | // If we have an entry in the MappedTypes table, then we have our answer. |
| 128 | Type *&Entry = MappedTypes[SrcTy]; |
| 129 | if (Entry) |
| 130 | return Entry == DstTy; |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 131 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 132 | // Two identical types are clearly isomorphic. Remember this |
| 133 | // non-speculatively. |
| 134 | if (DstTy == SrcTy) { |
| 135 | Entry = DstTy; |
Chris Lattner | fe677e9 | 2008-06-16 20:03:01 +0000 | [diff] [blame] | 136 | return true; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 137 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 138 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 139 | // 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] | 140 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 141 | // If this is an opaque struct type, special case it. |
| 142 | if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) { |
| 143 | // Mapping an opaque type to any struct, just keep the dest struct. |
| 144 | if (SSTy->isOpaque()) { |
| 145 | Entry = DstTy; |
| 146 | SpeculativeTypes.push_back(SrcTy); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 147 | return true; |
Chris Lattner | 9be1589 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 148 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 150 | // Mapping a non-opaque source type to an opaque dest. If this is the first |
| 151 | // type that we're mapping onto this destination type then we succeed. Keep |
Rafael Espindola | a96f235 | 2014-11-28 16:41:24 +0000 | [diff] [blame] | 152 | // the dest, but fill it in later. If this is the second (different) type |
| 153 | // that we're trying to map onto the same opaque type then we fail. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 154 | if (cast<StructType>(DstTy)->isOpaque()) { |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 155 | // We can only map one source type onto the opaque destination type. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 156 | if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second) |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 157 | return false; |
| 158 | SrcDefinitionsToResolve.push_back(SSTy); |
Rafael Espindola | a96f235 | 2014-11-28 16:41:24 +0000 | [diff] [blame] | 159 | SpeculativeTypes.push_back(SrcTy); |
| 160 | SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy)); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 161 | Entry = DstTy; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | } |
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 | // If the number of subtypes disagree between the two types, then we fail. |
| 167 | if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes()) |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 168 | return false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 169 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 170 | // Fail if any of the extra properties (e.g. array size) of the type disagree. |
| 171 | if (isa<IntegerType>(DstTy)) |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 172 | return false; // bitwidth disagrees. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 173 | if (PointerType *PT = dyn_cast<PointerType>(DstTy)) { |
| 174 | if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace()) |
| 175 | return false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 176 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 177 | } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) { |
| 178 | if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg()) |
| 179 | return false; |
| 180 | } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) { |
| 181 | StructType *SSTy = cast<StructType>(SrcTy); |
Chris Lattner | 44f7ab4 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 182 | if (DSTy->isLiteral() != SSTy->isLiteral() || |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 183 | DSTy->isPacked() != SSTy->isPacked()) |
| 184 | return false; |
| 185 | } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) { |
| 186 | if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements()) |
| 187 | return false; |
| 188 | } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
Joey Gouly | 5fad3e9 | 2013-01-10 10:49:36 +0000 | [diff] [blame] | 189 | if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 190 | return false; |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 191 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 192 | |
| 193 | // Otherwise, we speculate that these two types will line up and recursively |
| 194 | // check the subelements. |
| 195 | Entry = DstTy; |
| 196 | SpeculativeTypes.push_back(SrcTy); |
| 197 | |
Rafael Espindola | 290e2cc | 2014-11-25 14:35:53 +0000 | [diff] [blame] | 198 | for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I) |
| 199 | if (!areTypesIsomorphic(DstTy->getContainedType(I), |
| 200 | SrcTy->getContainedType(I))) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 201 | return false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 202 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 203 | // If everything seems to have lined up, then everything is great. |
| 204 | return true; |
| 205 | } |
| 206 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 207 | void TypeMapTy::linkDefinedTypeBodies() { |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 208 | SmallVector<Type *, 16> Elements; |
Rafael Espindola | c81c3f5 | 2014-11-25 15:33:40 +0000 | [diff] [blame] | 209 | for (StructType *SrcSTy : SrcDefinitionsToResolve) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 210 | StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]); |
Rafael Espindola | c81c3f5 | 2014-11-25 15:33:40 +0000 | [diff] [blame] | 211 | assert(DstSTy->isOpaque()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 212 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 213 | // Map the body of the source type over to a new body for the dest type. |
| 214 | Elements.resize(SrcSTy->getNumElements()); |
Rafael Espindola | 290e2cc | 2014-11-25 14:35:53 +0000 | [diff] [blame] | 215 | for (unsigned I = 0, E = Elements.size(); I != E; ++I) |
Rafael Espindola | c81c3f5 | 2014-11-25 15:33:40 +0000 | [diff] [blame] | 216 | Elements[I] = get(SrcSTy->getElementType(I)); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 217 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 218 | DstSTy->setBody(Elements, SrcSTy->isPacked()); |
Rafael Espindola | a5b9e1c | 2015-03-06 00:50:21 +0000 | [diff] [blame] | 219 | DstStructTypesSet.switchToNonOpaque(DstSTy); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 220 | } |
Rafael Espindola | c81c3f5 | 2014-11-25 15:33:40 +0000 | [diff] [blame] | 221 | SrcDefinitionsToResolve.clear(); |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 222 | DstResolvedOpaqueTypes.clear(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 225 | void TypeMapTy::finishType(StructType *DTy, StructType *STy, |
| 226 | ArrayRef<Type *> ETypes) { |
| 227 | DTy->setBody(ETypes, STy->isPacked()); |
Rafael Espindola | c81c3f5 | 2014-11-25 15:33:40 +0000 | [diff] [blame] | 228 | |
| 229 | // Steal STy's name. |
| 230 | if (STy->hasName()) { |
| 231 | SmallString<16> TmpName = STy->getName(); |
| 232 | STy->setName(""); |
| 233 | DTy->setName(TmpName); |
| 234 | } |
| 235 | |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 236 | DstStructTypesSet.addNonOpaque(DTy); |
| 237 | } |
| 238 | |
| 239 | Type *TypeMapTy::get(Type *Ty) { |
| 240 | SmallPtrSet<StructType *, 8> Visited; |
| 241 | return get(Ty, Visited); |
| 242 | } |
| 243 | |
| 244 | Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) { |
| 245 | // If we already have an entry for this type, return it. |
| 246 | Type **Entry = &MappedTypes[Ty]; |
| 247 | if (*Entry) |
| 248 | return *Entry; |
| 249 | |
| 250 | // These are types that LLVM itself will unique. |
| 251 | bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral(); |
| 252 | |
| 253 | #ifndef NDEBUG |
| 254 | if (!IsUniqued) { |
| 255 | for (auto &Pair : MappedTypes) { |
| 256 | assert(!(Pair.first != Ty && Pair.second == Ty) && |
| 257 | "mapping to a source type"); |
| 258 | } |
| 259 | } |
| 260 | #endif |
| 261 | |
| 262 | if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) { |
| 263 | StructType *DTy = StructType::create(Ty->getContext()); |
| 264 | return *Entry = DTy; |
| 265 | } |
| 266 | |
| 267 | // If this is not a recursive type, then just map all of the elements and |
| 268 | // then rebuild the type from inside out. |
| 269 | SmallVector<Type *, 4> ElementTypes; |
| 270 | |
| 271 | // If there are no element types to map, then the type is itself. This is |
| 272 | // true for the anonymous {} struct, things like 'float', integers, etc. |
| 273 | if (Ty->getNumContainedTypes() == 0 && IsUniqued) |
| 274 | return *Entry = Ty; |
| 275 | |
| 276 | // Remap all of the elements, keeping track of whether any of them change. |
| 277 | bool AnyChange = false; |
| 278 | ElementTypes.resize(Ty->getNumContainedTypes()); |
| 279 | for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) { |
| 280 | ElementTypes[I] = get(Ty->getContainedType(I), Visited); |
| 281 | AnyChange |= ElementTypes[I] != Ty->getContainedType(I); |
| 282 | } |
| 283 | |
| 284 | // If we found our type while recursively processing stuff, just use it. |
| 285 | Entry = &MappedTypes[Ty]; |
| 286 | if (*Entry) { |
| 287 | if (auto *DTy = dyn_cast<StructType>(*Entry)) { |
| 288 | if (DTy->isOpaque()) { |
| 289 | auto *STy = cast<StructType>(Ty); |
| 290 | finishType(DTy, STy, ElementTypes); |
| 291 | } |
| 292 | } |
| 293 | return *Entry; |
| 294 | } |
| 295 | |
| 296 | // If all of the element types mapped directly over and the type is not |
| 297 | // a nomed struct, then the type is usable as-is. |
| 298 | if (!AnyChange && IsUniqued) |
| 299 | return *Entry = Ty; |
| 300 | |
| 301 | // Otherwise, rebuild a modified type. |
| 302 | switch (Ty->getTypeID()) { |
| 303 | default: |
| 304 | llvm_unreachable("unknown derived type to remap"); |
| 305 | case Type::ArrayTyID: |
| 306 | return *Entry = ArrayType::get(ElementTypes[0], |
| 307 | cast<ArrayType>(Ty)->getNumElements()); |
| 308 | case Type::VectorTyID: |
| 309 | return *Entry = VectorType::get(ElementTypes[0], |
| 310 | cast<VectorType>(Ty)->getNumElements()); |
| 311 | case Type::PointerTyID: |
| 312 | return *Entry = PointerType::get(ElementTypes[0], |
| 313 | cast<PointerType>(Ty)->getAddressSpace()); |
| 314 | case Type::FunctionTyID: |
| 315 | return *Entry = FunctionType::get(ElementTypes[0], |
| 316 | makeArrayRef(ElementTypes).slice(1), |
| 317 | cast<FunctionType>(Ty)->isVarArg()); |
| 318 | case Type::StructTyID: { |
| 319 | auto *STy = cast<StructType>(Ty); |
| 320 | bool IsPacked = STy->isPacked(); |
| 321 | if (IsUniqued) |
| 322 | return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked); |
| 323 | |
| 324 | // If the type is opaque, we can just use it directly. |
| 325 | if (STy->isOpaque()) { |
| 326 | DstStructTypesSet.addOpaque(STy); |
| 327 | return *Entry = Ty; |
| 328 | } |
| 329 | |
| 330 | if (StructType *OldT = |
| 331 | DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) { |
| 332 | STy->setName(""); |
| 333 | return *Entry = OldT; |
| 334 | } |
| 335 | |
| 336 | if (!AnyChange) { |
| 337 | DstStructTypesSet.addNonOpaque(STy); |
| 338 | return *Entry = Ty; |
| 339 | } |
| 340 | |
| 341 | StructType *DTy = StructType::create(Ty->getContext()); |
| 342 | finishType(DTy, STy, ElementTypes); |
| 343 | return *Entry = DTy; |
| 344 | } |
| 345 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 348 | //===----------------------------------------------------------------------===// |
| 349 | // ModuleLinker implementation. |
| 350 | //===----------------------------------------------------------------------===// |
| 351 | |
| 352 | namespace { |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 353 | class ModuleLinker; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 354 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 355 | /// Creates prototypes for functions that are lazily linked on the fly. This |
| 356 | /// speeds up linking for modules with many/ lazily linked functions of which |
| 357 | /// few get used. |
David Blaikie | 437aafd | 2015-10-19 22:15:55 +0000 | [diff] [blame] | 358 | class ValueMaterializerTy final : public ValueMaterializer { |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 359 | ModuleLinker *ModLinker; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 360 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 361 | public: |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 362 | ValueMaterializerTy(ModuleLinker *ModLinker) : ModLinker(ModLinker) {} |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 363 | |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 364 | Value *materializeDeclFor(Value *V) override; |
| 365 | void materializeInitFor(GlobalValue *New, GlobalValue *Old) override; |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | class LinkDiagnosticInfo : public DiagnosticInfo { |
| 369 | const Twine &Msg; |
| 370 | |
| 371 | public: |
| 372 | LinkDiagnosticInfo(DiagnosticSeverity Severity, const Twine &Msg); |
| 373 | void print(DiagnosticPrinter &DP) const override; |
| 374 | }; |
| 375 | LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity, |
| 376 | const Twine &Msg) |
| 377 | : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {} |
| 378 | void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; } |
| 379 | |
| 380 | /// This is an implementation class for the LinkModules function, which is the |
| 381 | /// entrypoint for this file. |
| 382 | class ModuleLinker { |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 383 | Module &DstM; |
| 384 | Module &SrcM; |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 385 | |
| 386 | TypeMapTy TypeMap; |
| 387 | ValueMaterializerTy ValMaterializer; |
| 388 | |
| 389 | /// Mapping of values from what they used to be in Src, to what they are now |
| 390 | /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead |
| 391 | /// due to the use of Value handles which the Linker doesn't actually need, |
| 392 | /// but this allows us to reuse the ValueMapper code. |
| 393 | ValueToValueMapTy ValueMap; |
| 394 | |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 395 | SetVector<GlobalValue *> ValuesToLink; |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 396 | |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 397 | DiagnosticHandlerFunction DiagnosticHandler; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 398 | |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 399 | /// For symbol clashes, prefer those from Src. |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 400 | unsigned Flags; |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 401 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 402 | /// Function index passed into ModuleLinker for using in function |
| 403 | /// importing/exporting handling. |
Mehdi Amini | 8220e8a | 2015-11-23 01:59:16 +0000 | [diff] [blame] | 404 | const FunctionInfoIndex *ImportIndex; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 405 | |
| 406 | /// Function to import from source module, all other functions are |
| 407 | /// imported as declarations instead of definitions. |
Mehdi Amini | ffe2e4a | 2015-12-02 04:34:28 +0000 | [diff] [blame] | 408 | DenseSet<const GlobalValue *> *ImportFunction; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 409 | |
| 410 | /// Set to true if the given FunctionInfoIndex contains any functions |
| 411 | /// from this source module, in which case we must conservatively assume |
| 412 | /// that any of its functions may be imported into another module |
| 413 | /// as part of a different backend compilation process. |
Rafael Espindola | af71476 | 2015-12-01 23:06:26 +0000 | [diff] [blame] | 414 | bool HasExportedFunctions = false; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 415 | |
Teresa Johnson | 1063293 | 2015-11-06 17:50:53 +0000 | [diff] [blame] | 416 | /// Set to true when all global value body linking is complete (including |
| 417 | /// lazy linking). Used to prevent metadata linking from creating new |
| 418 | /// references. |
Rafael Espindola | af71476 | 2015-12-01 23:06:26 +0000 | [diff] [blame] | 419 | bool DoneLinkingBodies = false; |
Teresa Johnson | 1063293 | 2015-11-06 17:50:53 +0000 | [diff] [blame] | 420 | |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 421 | bool HasError = false; |
| 422 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 423 | public: |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 424 | ModuleLinker(Module &DstM, Linker::IdentifiedStructTypeSet &Set, Module &SrcM, |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 425 | DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags, |
Mehdi Amini | 8220e8a | 2015-11-23 01:59:16 +0000 | [diff] [blame] | 426 | const FunctionInfoIndex *Index = nullptr, |
Mehdi Amini | 7471cf8 | 2015-12-03 02:37:30 +0000 | [diff] [blame] | 427 | DenseSet<const GlobalValue *> *FunctionsToImport = nullptr) |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 428 | : DstM(DstM), SrcM(SrcM), TypeMap(Set), ValMaterializer(this), |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 429 | DiagnosticHandler(DiagnosticHandler), Flags(Flags), ImportIndex(Index), |
Mehdi Amini | 7471cf8 | 2015-12-03 02:37:30 +0000 | [diff] [blame] | 430 | ImportFunction(FunctionsToImport) { |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 431 | assert((ImportIndex || !ImportFunction) && |
| 432 | "Expect a FunctionInfoIndex when importing"); |
| 433 | // If we have a FunctionInfoIndex but no function to import, |
| 434 | // then this is the primary module being compiled in a ThinLTO |
| 435 | // backend compilation, and we need to see if it has functions that |
| 436 | // may be exported to another backend compilation. |
| 437 | if (ImportIndex && !ImportFunction) |
Mehdi Amini | 9abe108 | 2015-12-03 02:37:23 +0000 | [diff] [blame] | 438 | HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 439 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 440 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 441 | bool run(); |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 442 | Value *materializeDeclFor(Value *V); |
| 443 | void materializeInitFor(GlobalValue *New, GlobalValue *Old); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 444 | |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 445 | private: |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 446 | bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; } |
| 447 | bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; } |
| 448 | bool shouldInternalizeLinkedSymbols() { |
| 449 | return Flags & Linker::InternalizeLinkedSymbols; |
| 450 | } |
| 451 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 452 | /// Handles cloning of a global values from the source module into |
| 453 | /// the destination module, including setting the attributes and visibility. |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 454 | GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, |
Rafael Espindola | f3518c9 | 2015-12-02 19:30:52 +0000 | [diff] [blame] | 455 | const GlobalValue *DGV, bool ForDefinition); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 456 | |
| 457 | /// Check if we should promote the given local value to global scope. |
| 458 | bool doPromoteLocalToGlobal(const GlobalValue *SGV); |
| 459 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 460 | bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest, |
| 461 | const GlobalValue &Src); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 462 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 463 | /// Helper method for setting a message and returning an error code. |
| 464 | bool emitError(const Twine &Message) { |
| 465 | DiagnosticHandler(LinkDiagnosticInfo(DS_Error, Message)); |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 466 | HasError = true; |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 467 | return true; |
| 468 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 469 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 470 | void emitWarning(const Twine &Message) { |
| 471 | DiagnosticHandler(LinkDiagnosticInfo(DS_Warning, Message)); |
| 472 | } |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 473 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 474 | bool getComdatLeader(Module &M, StringRef ComdatName, |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 475 | const GlobalVariable *&GVar); |
| 476 | bool computeResultingSelectionKind(StringRef ComdatName, |
| 477 | Comdat::SelectionKind Src, |
| 478 | Comdat::SelectionKind Dst, |
| 479 | Comdat::SelectionKind &Result, |
| 480 | bool &LinkFromSrc); |
| 481 | std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>> |
| 482 | ComdatsChosen; |
| 483 | bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK, |
| 484 | bool &LinkFromSrc); |
Teresa Johnson | 2d5fb8c | 2015-11-10 21:09:06 +0000 | [diff] [blame] | 485 | // Keep track of the global value members of each comdat in source. |
| 486 | DenseMap<const Comdat *, std::vector<GlobalValue *>> ComdatMembers; |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 487 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 488 | /// Given a global in the source module, return the global in the |
| 489 | /// destination module that is being linked to, if any. |
| 490 | GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) { |
| 491 | // If the source has no name it can't link. If it has local linkage, |
| 492 | // there is no name match-up going on. |
Teresa Johnson | b098f0c | 2015-11-24 19:46:58 +0000 | [diff] [blame] | 493 | if (!SrcGV->hasName() || GlobalValue::isLocalLinkage(getLinkage(SrcGV))) |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 494 | return nullptr; |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 495 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 496 | // Otherwise see if we have a match in the destination module's symtab. |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 497 | GlobalValue *DGV = DstM.getNamedValue(getName(SrcGV)); |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 498 | if (!DGV) |
| 499 | return nullptr; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 500 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 501 | // If we found a global with the same name in the dest module, but it has |
| 502 | // internal linkage, we are really not doing any linkage here. |
| 503 | if (DGV->hasLocalLinkage()) |
| 504 | return nullptr; |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 505 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 506 | // Otherwise, we do in fact link to the destination global. |
| 507 | return DGV; |
| 508 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 509 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 510 | void computeTypeMapping(); |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 511 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 512 | void upgradeMismatchedGlobalArray(StringRef Name); |
| 513 | void upgradeMismatchedGlobals(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 514 | |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 515 | bool linkIfNeeded(GlobalValue &GV); |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 516 | Constant *linkAppendingVarProto(GlobalVariable *DstGV, |
| 517 | const GlobalVariable *SrcGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 518 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 519 | Constant *linkGlobalValueProto(GlobalValue *GV); |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 520 | bool linkModuleFlagsMetadata(); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 521 | |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 522 | void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src); |
| 523 | bool linkFunctionBody(Function &Dst, Function &Src); |
| 524 | void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src); |
Rafael Espindola | e39cd5b | 2015-12-01 22:40:40 +0000 | [diff] [blame] | 525 | bool linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 526 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 527 | /// Functions that take care of cloning a specific global value type |
| 528 | /// into the destination module. |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 529 | GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar); |
| 530 | Function *copyFunctionProto(const Function *SF); |
| 531 | GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 532 | |
| 533 | /// Helper methods to check if we are importing from or potentially |
| 534 | /// exporting from the current source module. |
| 535 | bool isPerformingImport() { return ImportFunction != nullptr; } |
| 536 | bool isModuleExporting() { return HasExportedFunctions; } |
| 537 | |
| 538 | /// If we are importing from the source module, checks if we should |
| 539 | /// import SGV as a definition, otherwise import as a declaration. |
| 540 | bool doImportAsDefinition(const GlobalValue *SGV); |
| 541 | |
| 542 | /// Get the name for SGV that should be used in the linked destination |
| 543 | /// module. Specifically, this handles the case where we need to rename |
| 544 | /// a local that is being promoted to global scope. |
| 545 | std::string getName(const GlobalValue *SGV); |
| 546 | |
| 547 | /// Get the new linkage for SGV that should be used in the linked destination |
| 548 | /// module. Specifically, for ThinLTO importing or exporting it may need |
| 549 | /// to be adjusted. |
| 550 | GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV); |
| 551 | |
| 552 | /// Copies the necessary global value attributes and name from the source |
| 553 | /// to the newly cloned global value. |
| 554 | void copyGVAttributes(GlobalValue *NewGV, const GlobalValue *SrcGV); |
| 555 | |
| 556 | /// Updates the visibility for the new global cloned from the source |
| 557 | /// and, if applicable, linked with an existing destination global. |
| 558 | /// Handles visibility change required for promoted locals. |
| 559 | void setVisibility(GlobalValue *NewGV, const GlobalValue *SGV, |
| 560 | const GlobalValue *DGV = nullptr); |
| 561 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 562 | void linkNamedMDNodes(); |
| 563 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 564 | } |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 565 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 566 | /// The LLVM SymbolTable class autorenames globals that conflict in the symbol |
| 567 | /// table. This is good for all clients except for us. Go through the trouble |
| 568 | /// to force this back. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 569 | static void forceRenaming(GlobalValue *GV, StringRef Name) { |
| 570 | // If the global doesn't force its name or if it already has the right name, |
| 571 | // there is nothing for us to do. |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 572 | // Note that any required local to global promotion should already be done, |
| 573 | // so promoted locals will not skip this handling as their linkage is no |
| 574 | // longer local. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 575 | if (GV->hasLocalLinkage() || GV->getName() == Name) |
| 576 | return; |
| 577 | |
| 578 | Module *M = GV->getParent(); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 579 | |
| 580 | // If there is a conflict, rename the conflict. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 581 | if (GlobalValue *ConflictGV = M->getNamedValue(Name)) { |
Chris Lattner | 2a8d2e0 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 582 | GV->takeName(ConflictGV); |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 583 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 584 | assert(ConflictGV->getName() != Name && "forceRenaming didn't work"); |
Chris Lattner | 2a8d2e0 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 585 | } else { |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 586 | GV->setName(Name); // Force the name back |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 587 | } |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 588 | } |
Reid Spencer | 90246aa | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 589 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 590 | /// copy additional attributes (those not needed to construct a GlobalValue) |
| 591 | /// from the SrcGV to the DestGV. |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 592 | void ModuleLinker::copyGVAttributes(GlobalValue *NewGV, |
| 593 | const GlobalValue *SrcGV) { |
Rafael Espindola | 769efe6 | 2015-12-02 20:03:17 +0000 | [diff] [blame] | 594 | NewGV->copyAttributesFrom(SrcGV); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 595 | forceRenaming(NewGV, getName(SrcGV)); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 598 | bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) { |
| 599 | if (!isPerformingImport()) |
| 600 | return false; |
Teresa Johnson | 3cd8161 | 2015-11-10 18:20:11 +0000 | [diff] [blame] | 601 | auto *GA = dyn_cast<GlobalAlias>(SGV); |
| 602 | if (GA) { |
| 603 | if (GA->hasWeakAnyLinkage()) |
| 604 | return false; |
Rafael Espindola | 8934577 | 2015-11-26 19:22:59 +0000 | [diff] [blame] | 605 | const GlobalObject *GO = GA->getBaseObject(); |
| 606 | if (!GO->hasLinkOnceODRLinkage()) |
| 607 | return false; |
| 608 | return doImportAsDefinition(GO); |
Teresa Johnson | 3cd8161 | 2015-11-10 18:20:11 +0000 | [diff] [blame] | 609 | } |
Teresa Johnson | dfbebc3 | 2015-11-10 18:26:31 +0000 | [diff] [blame] | 610 | // Always import GlobalVariable definitions, except for the special |
| 611 | // case of WeakAny which are imported as ExternalWeak declarations |
| 612 | // (see comments in ModuleLinker::getLinkage). The linkage changes |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 613 | // described in ModuleLinker::getLinkage ensure the correct behavior (e.g. |
| 614 | // global variables with external linkage are transformed to |
Teresa Johnson | 3cd8161 | 2015-11-10 18:20:11 +0000 | [diff] [blame] | 615 | // available_externally definitions, which are ultimately turned into |
| 616 | // declarations after the EliminateAvailableExternally pass). |
Craig Topper | 66059c9 | 2015-11-18 07:07:59 +0000 | [diff] [blame] | 617 | if (isa<GlobalVariable>(SGV) && !SGV->isDeclaration() && |
Teresa Johnson | 3cd8161 | 2015-11-10 18:20:11 +0000 | [diff] [blame] | 618 | !SGV->hasWeakAnyLinkage()) |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 619 | return true; |
| 620 | // Only import the function requested for importing. |
| 621 | auto *SF = dyn_cast<Function>(SGV); |
Mehdi Amini | ffe2e4a | 2015-12-02 04:34:28 +0000 | [diff] [blame] | 622 | if (SF && ImportFunction->count(SF)) |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 623 | return true; |
| 624 | // Otherwise no. |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | bool ModuleLinker::doPromoteLocalToGlobal(const GlobalValue *SGV) { |
| 629 | assert(SGV->hasLocalLinkage()); |
| 630 | // Both the imported references and the original local variable must |
| 631 | // be promoted. |
| 632 | if (!isPerformingImport() && !isModuleExporting()) |
| 633 | return false; |
| 634 | |
| 635 | // Local const variables never need to be promoted unless they are address |
| 636 | // taken. The imported uses can simply use the clone created in this module. |
| 637 | // For now we are conservative in determining which variables are not |
| 638 | // address taken by checking the unnamed addr flag. To be more aggressive, |
| 639 | // the address taken information must be checked earlier during parsing |
| 640 | // of the module and recorded in the function index for use when importing |
| 641 | // from that module. |
| 642 | auto *GVar = dyn_cast<GlobalVariable>(SGV); |
| 643 | if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr()) |
| 644 | return false; |
| 645 | |
| 646 | // Eventually we only need to promote functions in the exporting module that |
| 647 | // are referenced by a potentially exported function (i.e. one that is in the |
| 648 | // function index). |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | std::string ModuleLinker::getName(const GlobalValue *SGV) { |
| 653 | // For locals that must be promoted to global scope, ensure that |
| 654 | // the promoted name uniquely identifies the copy in the original module, |
| 655 | // using the ID assigned during combined index creation. When importing, |
| 656 | // we rename all locals (not just those that are promoted) in order to |
| 657 | // avoid naming conflicts between locals imported from different modules. |
| 658 | if (SGV->hasLocalLinkage() && |
| 659 | (doPromoteLocalToGlobal(SGV) || isPerformingImport())) |
| 660 | return FunctionInfoIndex::getGlobalNameForLocal( |
| 661 | SGV->getName(), |
| 662 | ImportIndex->getModuleId(SGV->getParent()->getModuleIdentifier())); |
| 663 | return SGV->getName(); |
| 664 | } |
| 665 | |
| 666 | GlobalValue::LinkageTypes ModuleLinker::getLinkage(const GlobalValue *SGV) { |
| 667 | // Any local variable that is referenced by an exported function needs |
| 668 | // to be promoted to global scope. Since we don't currently know which |
| 669 | // functions reference which local variables/functions, we must treat |
| 670 | // all as potentially exported if this module is exporting anything. |
| 671 | if (isModuleExporting()) { |
| 672 | if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV)) |
| 673 | return GlobalValue::ExternalLinkage; |
| 674 | return SGV->getLinkage(); |
| 675 | } |
| 676 | |
| 677 | // Otherwise, if we aren't importing, no linkage change is needed. |
| 678 | if (!isPerformingImport()) |
| 679 | return SGV->getLinkage(); |
| 680 | |
| 681 | switch (SGV->getLinkage()) { |
| 682 | case GlobalValue::ExternalLinkage: |
| 683 | // External defnitions are converted to available_externally |
| 684 | // definitions upon import, so that they are available for inlining |
| 685 | // and/or optimization, but are turned into declarations later |
| 686 | // during the EliminateAvailableExternally pass. |
Teresa Johnson | 3cd8161 | 2015-11-10 18:20:11 +0000 | [diff] [blame] | 687 | if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 688 | return GlobalValue::AvailableExternallyLinkage; |
| 689 | // An imported external declaration stays external. |
| 690 | return SGV->getLinkage(); |
| 691 | |
| 692 | case GlobalValue::AvailableExternallyLinkage: |
| 693 | // An imported available_externally definition converts |
| 694 | // to external if imported as a declaration. |
| 695 | if (!doImportAsDefinition(SGV)) |
| 696 | return GlobalValue::ExternalLinkage; |
| 697 | // An imported available_externally declaration stays that way. |
| 698 | return SGV->getLinkage(); |
| 699 | |
| 700 | case GlobalValue::LinkOnceAnyLinkage: |
| 701 | case GlobalValue::LinkOnceODRLinkage: |
| 702 | // These both stay the same when importing the definition. |
| 703 | // The ThinLTO pass will eventually force-import their definitions. |
| 704 | return SGV->getLinkage(); |
| 705 | |
| 706 | case GlobalValue::WeakAnyLinkage: |
| 707 | // Can't import weak_any definitions correctly, or we might change the |
| 708 | // program semantics, since the linker will pick the first weak_any |
| 709 | // definition and importing would change the order they are seen by the |
| 710 | // linker. The module linking caller needs to enforce this. |
| 711 | assert(!doImportAsDefinition(SGV)); |
| 712 | // If imported as a declaration, it becomes external_weak. |
| 713 | return GlobalValue::ExternalWeakLinkage; |
| 714 | |
| 715 | case GlobalValue::WeakODRLinkage: |
| 716 | // For weak_odr linkage, there is a guarantee that all copies will be |
| 717 | // equivalent, so the issue described above for weak_any does not exist, |
| 718 | // and the definition can be imported. It can be treated similarly |
| 719 | // to an imported externally visible global value. |
Teresa Johnson | 3cd8161 | 2015-11-10 18:20:11 +0000 | [diff] [blame] | 720 | if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 721 | return GlobalValue::AvailableExternallyLinkage; |
| 722 | else |
| 723 | return GlobalValue::ExternalLinkage; |
| 724 | |
| 725 | case GlobalValue::AppendingLinkage: |
| 726 | // It would be incorrect to import an appending linkage variable, |
| 727 | // since it would cause global constructors/destructors to be |
| 728 | // executed multiple times. This should have already been handled |
Teresa Johnson | 1e20a65 | 2015-12-03 18:20:05 +0000 | [diff] [blame] | 729 | // by linkIfNeeded, and we will assert in shouldLinkFromSource |
| 730 | // if we try to import, so we simply return AppendingLinkage here |
| 731 | // as this helper is called more widely in getLinkedToGlobal. |
| 732 | return GlobalValue::AppendingLinkage; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 733 | |
| 734 | case GlobalValue::InternalLinkage: |
| 735 | case GlobalValue::PrivateLinkage: |
| 736 | // If we are promoting the local to global scope, it is handled |
| 737 | // similarly to a normal externally visible global. |
| 738 | if (doPromoteLocalToGlobal(SGV)) { |
Teresa Johnson | 3cd8161 | 2015-11-10 18:20:11 +0000 | [diff] [blame] | 739 | if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 740 | return GlobalValue::AvailableExternallyLinkage; |
| 741 | else |
| 742 | return GlobalValue::ExternalLinkage; |
| 743 | } |
| 744 | // A non-promoted imported local definition stays local. |
| 745 | // The ThinLTO pass will eventually force-import their definitions. |
| 746 | return SGV->getLinkage(); |
| 747 | |
| 748 | case GlobalValue::ExternalWeakLinkage: |
| 749 | // External weak doesn't apply to definitions, must be a declaration. |
| 750 | assert(!doImportAsDefinition(SGV)); |
| 751 | // Linkage stays external_weak. |
| 752 | return SGV->getLinkage(); |
| 753 | |
| 754 | case GlobalValue::CommonLinkage: |
| 755 | // Linkage stays common on definitions. |
| 756 | // The ThinLTO pass will eventually force-import their definitions. |
| 757 | return SGV->getLinkage(); |
| 758 | } |
| 759 | |
| 760 | llvm_unreachable("unknown linkage type"); |
| 761 | } |
| 762 | |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 763 | /// Loop through the global variables in the src module and merge them into the |
| 764 | /// dest module. |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 765 | GlobalVariable * |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 766 | ModuleLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) { |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 767 | // No linking to be performed or linking from the source: simply create an |
| 768 | // identical version of the symbol over in the dest module... the |
| 769 | // initializer will be filled in later by LinkGlobalInits. |
Rafael Espindola | f3518c9 | 2015-12-02 19:30:52 +0000 | [diff] [blame] | 770 | GlobalVariable *NewDGV = |
| 771 | new GlobalVariable(DstM, TypeMap.get(SGVar->getType()->getElementType()), |
| 772 | SGVar->isConstant(), GlobalValue::ExternalLinkage, |
| 773 | /*init*/ nullptr, getName(SGVar), |
| 774 | /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(), |
| 775 | SGVar->getType()->getAddressSpace()); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 776 | |
| 777 | return NewDGV; |
| 778 | } |
| 779 | |
| 780 | /// Link the function in the source module into the destination module if |
| 781 | /// needed, setting up mapping information. |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 782 | Function *ModuleLinker::copyFunctionProto(const Function *SF) { |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 783 | // If there is no linkage to be performed or we are linking from the source, |
| 784 | // bring SF over. |
Rafael Espindola | f3518c9 | 2015-12-02 19:30:52 +0000 | [diff] [blame] | 785 | return Function::Create(TypeMap.get(SF->getFunctionType()), |
| 786 | GlobalValue::ExternalLinkage, getName(SF), &DstM); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /// Set up prototypes for any aliases that come over from the source module. |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 790 | GlobalValue *ModuleLinker::copyGlobalAliasProto(const GlobalAlias *SGA) { |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 791 | // If there is no linkage to be performed or we're linking from the source, |
| 792 | // bring over SGA. |
David Blaikie | 6614d8d | 2015-09-14 20:29:26 +0000 | [diff] [blame] | 793 | auto *Ty = TypeMap.get(SGA->getValueType()); |
| 794 | return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(), |
Rafael Espindola | f3518c9 | 2015-12-02 19:30:52 +0000 | [diff] [blame] | 795 | GlobalValue::ExternalLinkage, getName(SGA), &DstM); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Rafael Espindola | eb5e0a7 | 2015-11-29 14:33:06 +0000 | [diff] [blame] | 798 | static GlobalValue::VisibilityTypes |
| 799 | getMinVisibility(GlobalValue::VisibilityTypes A, |
| 800 | GlobalValue::VisibilityTypes B) { |
| 801 | if (A == GlobalValue::HiddenVisibility || B == GlobalValue::HiddenVisibility) |
| 802 | return GlobalValue::HiddenVisibility; |
| 803 | if (A == GlobalValue::ProtectedVisibility || |
| 804 | B == GlobalValue::ProtectedVisibility) |
| 805 | return GlobalValue::ProtectedVisibility; |
| 806 | return GlobalValue::DefaultVisibility; |
| 807 | } |
| 808 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 809 | void ModuleLinker::setVisibility(GlobalValue *NewGV, const GlobalValue *SGV, |
| 810 | const GlobalValue *DGV) { |
| 811 | GlobalValue::VisibilityTypes Visibility = SGV->getVisibility(); |
| 812 | if (DGV) |
Rafael Espindola | eb5e0a7 | 2015-11-29 14:33:06 +0000 | [diff] [blame] | 813 | Visibility = getMinVisibility(DGV->getVisibility(), Visibility); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 814 | // For promoted locals, mark them hidden so that they can later be |
| 815 | // stripped from the symbol table to reduce bloat. |
| 816 | if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV)) |
| 817 | Visibility = GlobalValue::HiddenVisibility; |
| 818 | NewGV->setVisibility(Visibility); |
| 819 | } |
| 820 | |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 821 | GlobalValue *ModuleLinker::copyGlobalValueProto(const GlobalValue *SGV, |
Rafael Espindola | f3518c9 | 2015-12-02 19:30:52 +0000 | [diff] [blame] | 822 | const GlobalValue *DGV, |
| 823 | bool ForDefinition) { |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 824 | GlobalValue *NewGV; |
Rafael Espindola | f3518c9 | 2015-12-02 19:30:52 +0000 | [diff] [blame] | 825 | if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) { |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 826 | NewGV = copyGlobalVariableProto(SGVar); |
Rafael Espindola | f3518c9 | 2015-12-02 19:30:52 +0000 | [diff] [blame] | 827 | } else if (auto *SF = dyn_cast<Function>(SGV)) { |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 828 | NewGV = copyFunctionProto(SF); |
Rafael Espindola | f3518c9 | 2015-12-02 19:30:52 +0000 | [diff] [blame] | 829 | } else { |
| 830 | if (ForDefinition) |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 831 | NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV)); |
Rafael Espindola | f3518c9 | 2015-12-02 19:30:52 +0000 | [diff] [blame] | 832 | else |
| 833 | NewGV = new GlobalVariable( |
| 834 | DstM, TypeMap.get(SGV->getType()->getElementType()), |
| 835 | /*isConstant*/ false, GlobalValue::ExternalLinkage, |
| 836 | /*init*/ nullptr, getName(SGV), |
| 837 | /*insertbefore*/ nullptr, SGV->getThreadLocalMode(), |
| 838 | SGV->getType()->getAddressSpace()); |
| 839 | } |
| 840 | |
| 841 | if (ForDefinition) |
| 842 | NewGV->setLinkage(getLinkage(SGV)); |
| 843 | else if (SGV->hasAvailableExternallyLinkage() || SGV->hasWeakLinkage() || |
| 844 | SGV->hasLinkOnceLinkage()) |
| 845 | NewGV->setLinkage(GlobalValue::ExternalWeakLinkage); |
| 846 | |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 847 | copyGVAttributes(NewGV, SGV); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 848 | setVisibility(NewGV, SGV, DGV); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 849 | return NewGV; |
| 850 | } |
| 851 | |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 852 | Value *ValueMaterializerTy::materializeDeclFor(Value *V) { |
| 853 | return ModLinker->materializeDeclFor(V); |
| 854 | } |
| 855 | |
| 856 | Value *ModuleLinker::materializeDeclFor(Value *V) { |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 857 | auto *SGV = dyn_cast<GlobalValue>(V); |
| 858 | if (!SGV) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 859 | return nullptr; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 860 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 861 | return linkGlobalValueProto(SGV); |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 864 | void ValueMaterializerTy::materializeInitFor(GlobalValue *New, |
| 865 | GlobalValue *Old) { |
| 866 | return ModLinker->materializeInitFor(New, Old); |
| 867 | } |
| 868 | |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 869 | static bool shouldLazyLink(const GlobalValue &GV) { |
| 870 | return GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() || |
| 871 | GV.hasAvailableExternallyLinkage(); |
| 872 | } |
| 873 | |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 874 | void ModuleLinker::materializeInitFor(GlobalValue *New, GlobalValue *Old) { |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 875 | if (auto *F = dyn_cast<Function>(New)) { |
| 876 | if (!F->isDeclaration()) |
| 877 | return; |
| 878 | } else if (auto *V = dyn_cast<GlobalVariable>(New)) { |
| 879 | if (V->hasInitializer()) |
| 880 | return; |
| 881 | } else { |
| 882 | auto *A = cast<GlobalAlias>(New); |
| 883 | if (A->getAliasee()) |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | if (Old->isDeclaration()) |
| 888 | return; |
| 889 | |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 890 | if (isPerformingImport() && !doImportAsDefinition(Old)) |
| 891 | return; |
| 892 | |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 893 | if (!ValuesToLink.count(Old) && !shouldLazyLink(*Old)) |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 894 | return; |
| 895 | |
Rafael Espindola | e39cd5b | 2015-12-01 22:40:40 +0000 | [diff] [blame] | 896 | linkGlobalValueBody(*New, *Old); |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 899 | bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName, |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 900 | const GlobalVariable *&GVar) { |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 901 | const GlobalValue *GVal = M.getNamedValue(ComdatName); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 902 | if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) { |
| 903 | GVal = GA->getBaseObject(); |
| 904 | if (!GVal) |
| 905 | // We cannot resolve the size of the aliasee yet. |
| 906 | return emitError("Linking COMDATs named '" + ComdatName + |
| 907 | "': COMDAT key involves incomputable alias size."); |
| 908 | } |
| 909 | |
| 910 | GVar = dyn_cast_or_null<GlobalVariable>(GVal); |
| 911 | if (!GVar) |
| 912 | return emitError( |
| 913 | "Linking COMDATs named '" + ComdatName + |
| 914 | "': GlobalVariable required for data dependent selection!"); |
| 915 | |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName, |
| 920 | Comdat::SelectionKind Src, |
| 921 | Comdat::SelectionKind Dst, |
| 922 | Comdat::SelectionKind &Result, |
| 923 | bool &LinkFromSrc) { |
| 924 | // The ability to mix Comdat::SelectionKind::Any with |
| 925 | // Comdat::SelectionKind::Largest is a behavior that comes from COFF. |
| 926 | bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any || |
| 927 | Dst == Comdat::SelectionKind::Largest; |
| 928 | bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any || |
| 929 | Src == Comdat::SelectionKind::Largest; |
| 930 | if (DstAnyOrLargest && SrcAnyOrLargest) { |
| 931 | if (Dst == Comdat::SelectionKind::Largest || |
| 932 | Src == Comdat::SelectionKind::Largest) |
| 933 | Result = Comdat::SelectionKind::Largest; |
| 934 | else |
| 935 | Result = Comdat::SelectionKind::Any; |
| 936 | } else if (Src == Dst) { |
| 937 | Result = Dst; |
| 938 | } else { |
| 939 | return emitError("Linking COMDATs named '" + ComdatName + |
| 940 | "': invalid selection kinds!"); |
| 941 | } |
| 942 | |
| 943 | switch (Result) { |
| 944 | case Comdat::SelectionKind::Any: |
| 945 | // Go with Dst. |
| 946 | LinkFromSrc = false; |
| 947 | break; |
| 948 | case Comdat::SelectionKind::NoDuplicates: |
| 949 | return emitError("Linking COMDATs named '" + ComdatName + |
| 950 | "': noduplicates has been violated!"); |
| 951 | case Comdat::SelectionKind::ExactMatch: |
| 952 | case Comdat::SelectionKind::Largest: |
| 953 | case Comdat::SelectionKind::SameSize: { |
| 954 | const GlobalVariable *DstGV; |
| 955 | const GlobalVariable *SrcGV; |
| 956 | if (getComdatLeader(DstM, ComdatName, DstGV) || |
| 957 | getComdatLeader(SrcM, ComdatName, SrcGV)) |
| 958 | return true; |
| 959 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 960 | const DataLayout &DstDL = DstM.getDataLayout(); |
| 961 | const DataLayout &SrcDL = SrcM.getDataLayout(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 962 | uint64_t DstSize = |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 963 | DstDL.getTypeAllocSize(DstGV->getType()->getPointerElementType()); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 964 | uint64_t SrcSize = |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 965 | SrcDL.getTypeAllocSize(SrcGV->getType()->getPointerElementType()); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 966 | if (Result == Comdat::SelectionKind::ExactMatch) { |
| 967 | if (SrcGV->getInitializer() != DstGV->getInitializer()) |
| 968 | return emitError("Linking COMDATs named '" + ComdatName + |
| 969 | "': ExactMatch violated!"); |
| 970 | LinkFromSrc = false; |
| 971 | } else if (Result == Comdat::SelectionKind::Largest) { |
| 972 | LinkFromSrc = SrcSize > DstSize; |
| 973 | } else if (Result == Comdat::SelectionKind::SameSize) { |
| 974 | if (SrcSize != DstSize) |
| 975 | return emitError("Linking COMDATs named '" + ComdatName + |
| 976 | "': SameSize violated!"); |
| 977 | LinkFromSrc = false; |
| 978 | } else { |
| 979 | llvm_unreachable("unknown selection kind"); |
| 980 | } |
| 981 | break; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | return false; |
| 986 | } |
| 987 | |
| 988 | bool ModuleLinker::getComdatResult(const Comdat *SrcC, |
| 989 | Comdat::SelectionKind &Result, |
| 990 | bool &LinkFromSrc) { |
Rafael Espindola | b16196a | 2014-08-11 17:07:34 +0000 | [diff] [blame] | 991 | Comdat::SelectionKind SSK = SrcC->getSelectionKind(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 992 | StringRef ComdatName = SrcC->getName(); |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 993 | Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 994 | Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName); |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 995 | |
Rafael Espindola | b16196a | 2014-08-11 17:07:34 +0000 | [diff] [blame] | 996 | if (DstCI == ComdatSymTab.end()) { |
| 997 | // Use the comdat if it is only available in one of the modules. |
| 998 | LinkFromSrc = true; |
| 999 | Result = SSK; |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 1000 | return false; |
Rafael Espindola | b16196a | 2014-08-11 17:07:34 +0000 | [diff] [blame] | 1001 | } |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 1002 | |
| 1003 | const Comdat *DstC = &DstCI->second; |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 1004 | Comdat::SelectionKind DSK = DstC->getSelectionKind(); |
| 1005 | return computeResultingSelectionKind(ComdatName, SSK, DSK, Result, |
| 1006 | LinkFromSrc); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1007 | } |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1008 | |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1009 | bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc, |
| 1010 | const GlobalValue &Dest, |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1011 | const GlobalValue &Src) { |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 1012 | // Should we unconditionally use the Src? |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 1013 | if (shouldOverrideFromSrc()) { |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 1014 | LinkFromSrc = true; |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1018 | // We always have to add Src if it has appending linkage. |
| 1019 | if (Src.hasAppendingLinkage()) { |
Teresa Johnson | 1e20a65 | 2015-12-03 18:20:05 +0000 | [diff] [blame] | 1020 | // Should have prevented importing for appending linkage in linkIfNeeded. |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 1021 | assert(!isPerformingImport()); |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1022 | LinkFromSrc = true; |
| 1023 | return false; |
| 1024 | } |
| 1025 | |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 1026 | bool SrcIsDeclaration = Src.isDeclarationForLinker(); |
| 1027 | bool DestIsDeclaration = Dest.isDeclarationForLinker(); |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1028 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 1029 | if (isPerformingImport()) { |
| 1030 | if (isa<Function>(&Src)) { |
| 1031 | // For functions, LinkFromSrc iff this is the function requested |
| 1032 | // for importing. For variables, decide below normally. |
Mehdi Amini | ffe2e4a | 2015-12-02 04:34:28 +0000 | [diff] [blame] | 1033 | LinkFromSrc = ImportFunction->count(&Src); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 1034 | return false; |
| 1035 | } |
| 1036 | |
| 1037 | // Check if this is an alias with an already existing definition |
| 1038 | // in Dest, which must have come from a prior importing pass from |
| 1039 | // the same Src module. Unlike imported function and variable |
| 1040 | // definitions, which are imported as available_externally and are |
| 1041 | // not definitions for the linker, that is not a valid linkage for |
| 1042 | // imported aliases which must be definitions. Simply use the existing |
| 1043 | // Dest copy. |
| 1044 | if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) { |
| 1045 | assert(isa<GlobalAlias>(&Dest)); |
| 1046 | LinkFromSrc = false; |
| 1047 | return false; |
| 1048 | } |
| 1049 | } |
| 1050 | |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1051 | if (SrcIsDeclaration) { |
| 1052 | // If Src is external or if both Src & Dest are external.. Just link the |
| 1053 | // external globals, we aren't adding anything. |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1054 | if (Src.hasDLLImportStorageClass()) { |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1055 | // If one of GVs is marked as DLLImport, result should be dllimport'ed. |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1056 | LinkFromSrc = DestIsDeclaration; |
| 1057 | return false; |
| 1058 | } |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1059 | // If the Dest is weak, use the source linkage. |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1060 | LinkFromSrc = Dest.hasExternalWeakLinkage(); |
| 1061 | return false; |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1064 | if (DestIsDeclaration) { |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1065 | // If Dest is external but Src is not: |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1066 | LinkFromSrc = true; |
| 1067 | return false; |
| 1068 | } |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1069 | |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 1070 | if (Src.hasCommonLinkage()) { |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1071 | if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) { |
| 1072 | LinkFromSrc = true; |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 1073 | return false; |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | if (!Dest.hasCommonLinkage()) { |
| 1077 | LinkFromSrc = false; |
| 1078 | return false; |
| 1079 | } |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 1080 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1081 | const DataLayout &DL = Dest.getParent()->getDataLayout(); |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 1082 | uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType()); |
| 1083 | uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType()); |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1084 | LinkFromSrc = SrcSize > DestSize; |
| 1085 | return false; |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1088 | if (Src.isWeakForLinker()) { |
| 1089 | assert(!Dest.hasExternalWeakLinkage()); |
| 1090 | assert(!Dest.hasAvailableExternallyLinkage()); |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 1091 | |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1092 | if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) { |
| 1093 | LinkFromSrc = true; |
| 1094 | return false; |
| 1095 | } |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1096 | |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1097 | LinkFromSrc = false; |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 1098 | return false; |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | if (Dest.isWeakForLinker()) { |
| 1102 | assert(Src.hasExternalLinkage()); |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1103 | LinkFromSrc = true; |
| 1104 | return false; |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | assert(!Src.hasExternalWeakLinkage()); |
| 1108 | assert(!Dest.hasExternalWeakLinkage()); |
| 1109 | assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() && |
| 1110 | "Unexpected linkage type!"); |
| 1111 | return emitError("Linking globals named '" + Src.getName() + |
| 1112 | "': symbol multiply defined!"); |
| 1113 | } |
| 1114 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 1115 | /// Loop over all of the linked values to compute type mappings. For example, |
| 1116 | /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct |
| 1117 | /// types 'Foo' but one got renamed when the module was loaded into the same |
| 1118 | /// LLVMContext. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1119 | void ModuleLinker::computeTypeMapping() { |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1120 | for (GlobalValue &SGV : SrcM.globals()) { |
Rafael Espindola | c8a476e | 2014-11-25 04:26:19 +0000 | [diff] [blame] | 1121 | GlobalValue *DGV = getLinkedToGlobal(&SGV); |
| 1122 | if (!DGV) |
| 1123 | continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1124 | |
Rafael Espindola | c8a476e | 2014-11-25 04:26:19 +0000 | [diff] [blame] | 1125 | if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) { |
| 1126 | TypeMap.addTypeMapping(DGV->getType(), SGV.getType()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1127 | continue; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1128 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1129 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1130 | // Unify the element type of appending arrays. |
| 1131 | ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType()); |
Rafael Espindola | c8a476e | 2014-11-25 04:26:19 +0000 | [diff] [blame] | 1132 | ArrayType *SAT = cast<ArrayType>(SGV.getType()->getElementType()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1133 | TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType()); |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 1134 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1135 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1136 | for (GlobalValue &SGV : SrcM) { |
Rafael Espindola | c8a476e | 2014-11-25 04:26:19 +0000 | [diff] [blame] | 1137 | if (GlobalValue *DGV = getLinkedToGlobal(&SGV)) |
| 1138 | TypeMap.addTypeMapping(DGV->getType(), SGV.getType()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1139 | } |
Bill Wendling | 7b46461 | 2012-02-27 22:34:19 +0000 | [diff] [blame] | 1140 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1141 | for (GlobalValue &SGV : SrcM.aliases()) { |
Rafael Espindola | e96d7eb | 2014-11-25 04:43:59 +0000 | [diff] [blame] | 1142 | if (GlobalValue *DGV = getLinkedToGlobal(&SGV)) |
| 1143 | TypeMap.addTypeMapping(DGV->getType(), SGV.getType()); |
| 1144 | } |
| 1145 | |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 1146 | // Incorporate types by name, scanning all the types in the source module. |
| 1147 | // 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] | 1148 | // example. When the source module got loaded into the same LLVMContext, if |
| 1149 | // it had the same type, it would have been renamed to "%foo.42 = { i32 }". |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1150 | std::vector<StructType *> Types = SrcM.getIdentifiedStructTypes(); |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 1151 | for (StructType *ST : Types) { |
Rafael Espindola | 4d4c938 | 2014-12-01 19:08:07 +0000 | [diff] [blame] | 1152 | if (!ST->hasName()) |
| 1153 | continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1154 | |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 1155 | // 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] | 1156 | size_t DotPos = ST->getName().rfind('.'); |
| 1157 | if (DotPos == 0 || DotPos == StringRef::npos || |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1158 | ST->getName().back() == '.' || |
Rafael Espindola | 973b361 | 2014-12-01 19:17:46 +0000 | [diff] [blame] | 1159 | !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1]))) |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 1160 | continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1161 | |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 1162 | // Check to see if the destination module has a struct with the prefix name. |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1163 | StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos)); |
Rafael Espindola | 973b361 | 2014-12-01 19:17:46 +0000 | [diff] [blame] | 1164 | if (!DST) |
| 1165 | continue; |
| 1166 | |
| 1167 | // Don't use it if this actually came from the source module. They're in |
| 1168 | // the same LLVMContext after all. Also don't use it unless the type is |
| 1169 | // actually used in the destination module. This can happen in situations |
| 1170 | // like this: |
| 1171 | // |
| 1172 | // Module A Module B |
| 1173 | // -------- -------- |
| 1174 | // %Z = type { %A } %B = type { %C.1 } |
| 1175 | // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* } |
| 1176 | // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] } |
| 1177 | // %C = type { i8* } %B.3 = type { %C.1 } |
| 1178 | // |
| 1179 | // When we link Module B with Module A, the '%B' in Module B is |
| 1180 | // used. However, that would then use '%C.1'. But when we process '%C.1', |
| 1181 | // we prefer to take the '%C' version. So we are then left with both |
| 1182 | // '%C.1' and '%C' being used for the same types. This leads to some |
| 1183 | // variables using one type and some using the other. |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 1184 | if (TypeMap.DstStructTypesSet.hasType(DST)) |
Rafael Espindola | 973b361 | 2014-12-01 19:17:46 +0000 | [diff] [blame] | 1185 | TypeMap.addTypeMapping(DST, ST); |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1188 | // 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] | 1189 | // any 'opaque' types in the dest module that are now resolved. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1190 | TypeMap.linkDefinedTypeBodies(); |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Duncan P. N. Exon Smith | 09d84ad | 2014-08-12 16:46:37 +0000 | [diff] [blame] | 1193 | static void upgradeGlobalArray(GlobalVariable *GV) { |
| 1194 | ArrayType *ATy = cast<ArrayType>(GV->getType()->getElementType()); |
| 1195 | StructType *OldTy = cast<StructType>(ATy->getElementType()); |
| 1196 | assert(OldTy->getNumElements() == 2 && "Expected to upgrade from 2 elements"); |
| 1197 | |
| 1198 | // Get the upgraded 3 element type. |
| 1199 | PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo(); |
| 1200 | Type *Tys[3] = {OldTy->getElementType(0), OldTy->getElementType(1), |
| 1201 | VoidPtrTy}; |
| 1202 | StructType *NewTy = StructType::get(GV->getContext(), Tys, false); |
| 1203 | |
| 1204 | // Build new constants with a null third field filled in. |
| 1205 | Constant *OldInitC = GV->getInitializer(); |
| 1206 | ConstantArray *OldInit = dyn_cast<ConstantArray>(OldInitC); |
| 1207 | if (!OldInit && !isa<ConstantAggregateZero>(OldInitC)) |
| 1208 | // Invalid initializer; give up. |
| 1209 | return; |
| 1210 | std::vector<Constant *> Initializers; |
| 1211 | if (OldInit && OldInit->getNumOperands()) { |
| 1212 | Value *Null = Constant::getNullValue(VoidPtrTy); |
| 1213 | for (Use &U : OldInit->operands()) { |
| 1214 | ConstantStruct *Init = cast<ConstantStruct>(U.get()); |
| 1215 | Initializers.push_back(ConstantStruct::get( |
| 1216 | NewTy, Init->getOperand(0), Init->getOperand(1), Null, nullptr)); |
| 1217 | } |
| 1218 | } |
| 1219 | assert(Initializers.size() == ATy->getNumElements() && |
| 1220 | "Failed to copy all array elements"); |
| 1221 | |
| 1222 | // Replace the old GV with a new one. |
| 1223 | ATy = ArrayType::get(NewTy, Initializers.size()); |
| 1224 | Constant *NewInit = ConstantArray::get(ATy, Initializers); |
| 1225 | GlobalVariable *NewGV = new GlobalVariable( |
| 1226 | *GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "", |
| 1227 | GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(), |
| 1228 | GV->isExternallyInitialized()); |
| 1229 | NewGV->copyAttributesFrom(GV); |
| 1230 | NewGV->takeName(GV); |
| 1231 | assert(GV->use_empty() && "program cannot use initializer list"); |
| 1232 | GV->eraseFromParent(); |
| 1233 | } |
| 1234 | |
| 1235 | void ModuleLinker::upgradeMismatchedGlobalArray(StringRef Name) { |
| 1236 | // Look for the global arrays. |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1237 | auto *DstGV = dyn_cast_or_null<GlobalVariable>(DstM.getNamedValue(Name)); |
Duncan P. N. Exon Smith | 09d84ad | 2014-08-12 16:46:37 +0000 | [diff] [blame] | 1238 | if (!DstGV) |
| 1239 | return; |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1240 | auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM.getNamedValue(Name)); |
Duncan P. N. Exon Smith | 09d84ad | 2014-08-12 16:46:37 +0000 | [diff] [blame] | 1241 | if (!SrcGV) |
| 1242 | return; |
| 1243 | |
| 1244 | // Check if the types already match. |
| 1245 | auto *DstTy = cast<ArrayType>(DstGV->getType()->getElementType()); |
| 1246 | auto *SrcTy = |
| 1247 | cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType())); |
| 1248 | if (DstTy == SrcTy) |
| 1249 | return; |
| 1250 | |
| 1251 | // Grab the element types. We can only upgrade an array of a two-field |
| 1252 | // struct. Only bother if the other one has three-fields. |
| 1253 | auto *DstEltTy = cast<StructType>(DstTy->getElementType()); |
| 1254 | auto *SrcEltTy = cast<StructType>(SrcTy->getElementType()); |
| 1255 | if (DstEltTy->getNumElements() == 2 && SrcEltTy->getNumElements() == 3) { |
| 1256 | upgradeGlobalArray(DstGV); |
| 1257 | return; |
| 1258 | } |
| 1259 | if (DstEltTy->getNumElements() == 3 && SrcEltTy->getNumElements() == 2) |
| 1260 | upgradeGlobalArray(SrcGV); |
| 1261 | |
| 1262 | // We can't upgrade any other differences. |
| 1263 | } |
| 1264 | |
| 1265 | void ModuleLinker::upgradeMismatchedGlobals() { |
| 1266 | upgradeMismatchedGlobalArray("llvm.global_ctors"); |
| 1267 | upgradeMismatchedGlobalArray("llvm.global_dtors"); |
| 1268 | } |
| 1269 | |
Rafael Espindola | 6e8ab92 | 2015-12-01 17:17:04 +0000 | [diff] [blame] | 1270 | static void getArrayElements(const Constant *C, |
| 1271 | SmallVectorImpl<Constant *> &Dest) { |
| 1272 | unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements(); |
| 1273 | |
| 1274 | for (unsigned i = 0; i != NumElements; ++i) |
| 1275 | Dest.push_back(C->getAggregateElement(i)); |
| 1276 | } |
| 1277 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 1278 | /// If there were any appending global variables, link them together now. |
| 1279 | /// Return true on error. |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1280 | Constant *ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV, |
| 1281 | const GlobalVariable *SrcGV) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1282 | ArrayType *SrcTy = |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1283 | cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType())); |
| 1284 | Type *EltTy = SrcTy->getElementType(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1285 | |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1286 | if (DstGV) { |
| 1287 | ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1288 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1289 | if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) { |
| 1290 | emitError( |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1291 | "Linking globals named '" + SrcGV->getName() + |
| 1292 | "': can only link appending global with another appending global!"); |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1293 | return nullptr; |
| 1294 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1295 | |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1296 | // Check to see that they two arrays agree on type. |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1297 | if (EltTy != DstTy->getElementType()) { |
| 1298 | emitError("Appending variables with different element types!"); |
| 1299 | return nullptr; |
| 1300 | } |
| 1301 | if (DstGV->isConstant() != SrcGV->isConstant()) { |
| 1302 | emitError("Appending variables linked with different const'ness!"); |
| 1303 | return nullptr; |
| 1304 | } |
Rafael Espindola | fac3a01 | 2013-09-04 15:33:34 +0000 | [diff] [blame] | 1305 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1306 | if (DstGV->getAlignment() != SrcGV->getAlignment()) { |
| 1307 | emitError( |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1308 | "Appending variables with different alignment need to be linked!"); |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1309 | return nullptr; |
| 1310 | } |
Rafael Espindola | fac3a01 | 2013-09-04 15:33:34 +0000 | [diff] [blame] | 1311 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1312 | if (DstGV->getVisibility() != SrcGV->getVisibility()) { |
| 1313 | emitError( |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1314 | "Appending variables with different visibility need to be linked!"); |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1315 | return nullptr; |
| 1316 | } |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1317 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1318 | if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr()) { |
| 1319 | emitError( |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1320 | "Appending variables with different unnamed_addr need to be linked!"); |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1321 | return nullptr; |
| 1322 | } |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1323 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1324 | if (StringRef(DstGV->getSection()) != SrcGV->getSection()) { |
| 1325 | emitError( |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1326 | "Appending variables with different section name need to be linked!"); |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1327 | return nullptr; |
| 1328 | } |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1329 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1330 | |
Rafael Espindola | 6e8ab92 | 2015-12-01 17:17:04 +0000 | [diff] [blame] | 1331 | SmallVector<Constant *, 16> DstElements; |
| 1332 | if (DstGV) |
| 1333 | getArrayElements(DstGV->getInitializer(), DstElements); |
| 1334 | |
| 1335 | SmallVector<Constant *, 16> SrcElements; |
| 1336 | getArrayElements(SrcGV->getInitializer(), SrcElements); |
| 1337 | |
| 1338 | StringRef Name = SrcGV->getName(); |
| 1339 | bool IsNewStructor = |
| 1340 | (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") && |
| 1341 | cast<StructType>(EltTy)->getNumElements() == 3; |
| 1342 | if (IsNewStructor) |
| 1343 | SrcElements.erase( |
| 1344 | std::remove_if(SrcElements.begin(), SrcElements.end(), |
| 1345 | [this](Constant *E) { |
| 1346 | auto *Key = dyn_cast<GlobalValue>( |
| 1347 | E->getAggregateElement(2)->stripPointerCasts()); |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 1348 | return Key && !ValuesToLink.count(Key) && |
| 1349 | !shouldLazyLink(*Key); |
Rafael Espindola | 6e8ab92 | 2015-12-01 17:17:04 +0000 | [diff] [blame] | 1350 | }), |
| 1351 | SrcElements.end()); |
| 1352 | uint64_t NewSize = DstElements.size() + SrcElements.size(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1353 | ArrayType *NewType = ArrayType::get(EltTy, NewSize); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1355 | // Create the new global variable. |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1356 | GlobalVariable *NG = new GlobalVariable( |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1357 | DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(), |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1358 | /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(), |
| 1359 | SrcGV->getType()->getAddressSpace()); |
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 | // Propagate alignment, visibility and section info. |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1362 | copyGVAttributes(NG, SrcGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1363 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1364 | Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType())); |
| 1365 | |
| 1366 | // Stop recursion. |
| 1367 | ValueMap[SrcGV] = Ret; |
Anton Korobeynikov | e79f4c7 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 1368 | |
Rafael Espindola | 6e8ab92 | 2015-12-01 17:17:04 +0000 | [diff] [blame] | 1369 | for (auto *V : SrcElements) { |
| 1370 | DstElements.push_back( |
| 1371 | MapValue(V, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer)); |
| 1372 | } |
| 1373 | |
| 1374 | NG->setInitializer(ConstantArray::get(NewType, DstElements)); |
| 1375 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1376 | // Replace any uses of the two global variables with uses of the new |
| 1377 | // global. |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1378 | if (DstGV) { |
| 1379 | DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType())); |
| 1380 | DstGV->eraseFromParent(); |
| 1381 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1382 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1383 | return Ret; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1384 | } |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 1385 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1386 | Constant *ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1387 | GlobalValue *DGV = getLinkedToGlobal(SGV); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 1388 | |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1389 | // Handle the ultra special appending linkage case first. |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 1390 | assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage()); |
Teresa Johnson | 1e20a65 | 2015-12-03 18:20:05 +0000 | [diff] [blame] | 1391 | if (SGV->hasAppendingLinkage()) { |
| 1392 | // Should have prevented importing for appending linkage in linkIfNeeded. |
| 1393 | assert(!isPerformingImport()); |
Rafael Espindola | c945c8d | 2015-11-29 03:29:42 +0000 | [diff] [blame] | 1394 | return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV), |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1395 | cast<GlobalVariable>(SGV)); |
Teresa Johnson | 1e20a65 | 2015-12-03 18:20:05 +0000 | [diff] [blame] | 1396 | } |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1397 | |
| 1398 | bool LinkFromSrc = true; |
| 1399 | Comdat *C = nullptr; |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1400 | bool HasUnnamedAddr = SGV->hasUnnamedAddr(); |
| 1401 | |
Rafael Espindola | 8c04472 | 2015-12-02 22:22:24 +0000 | [diff] [blame] | 1402 | if (isPerformingImport() && !doImportAsDefinition(SGV)) { |
| 1403 | LinkFromSrc = false; |
| 1404 | } else if (const Comdat *SC = SGV->getComdat()) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1405 | Comdat::SelectionKind SK; |
| 1406 | std::tie(SK, LinkFromSrc) = ComdatsChosen[SC]; |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1407 | C = DstM.getOrInsertComdat(SC->getName()); |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1408 | C->setSelectionKind(SK); |
Rafael Espindola | 0a80da0 | 2015-12-02 20:57:33 +0000 | [diff] [blame] | 1409 | if (SGV->hasLocalLinkage()) |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1410 | LinkFromSrc = true; |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1411 | } else if (DGV) { |
| 1412 | if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV)) |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1413 | return nullptr; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 1416 | if (DGV) |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1417 | HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1418 | |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1419 | GlobalValue *NewGV; |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1420 | if (!LinkFromSrc && DGV) { |
Rafael Espindola | 26c2951 | 2014-12-05 17:53:15 +0000 | [diff] [blame] | 1421 | NewGV = DGV; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 1422 | // When linking from source we setVisibility from copyGlobalValueProto. |
| 1423 | setVisibility(NewGV, SGV, DGV); |
Rafael Espindola | 26c2951 | 2014-12-05 17:53:15 +0000 | [diff] [blame] | 1424 | } else { |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 1425 | // If we are done linking global value bodies (i.e. we are performing |
| 1426 | // metadata linking), don't link in the global value due to this |
| 1427 | // reference, simply map it to null. |
| 1428 | if (DoneLinkingBodies) |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1429 | return nullptr; |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 1430 | |
Rafael Espindola | 792b795 | 2015-12-03 14:48:20 +0000 | [diff] [blame] | 1431 | NewGV = copyGlobalValueProto(SGV, DGV, LinkFromSrc); |
Rafael Espindola | 26c2951 | 2014-12-05 17:53:15 +0000 | [diff] [blame] | 1432 | } |
Rafael Espindola | fe3842c | 2014-09-09 17:48:18 +0000 | [diff] [blame] | 1433 | |
Rafael Espindola | ad9d0ca | 2014-12-05 15:42:30 +0000 | [diff] [blame] | 1434 | NewGV->setUnnamedAddr(HasUnnamedAddr); |
Rafael Espindola | 439835a | 2014-12-05 00:09:02 +0000 | [diff] [blame] | 1435 | |
Rafael Espindola | ad9d0ca | 2014-12-05 15:42:30 +0000 | [diff] [blame] | 1436 | if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) { |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1437 | if (C && LinkFromSrc) |
Rafael Espindola | ad9d0ca | 2014-12-05 15:42:30 +0000 | [diff] [blame] | 1438 | NewGO->setComdat(C); |
| 1439 | |
| 1440 | if (DGV && DGV->hasCommonLinkage() && SGV->hasCommonLinkage()) |
| 1441 | NewGO->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment())); |
| 1442 | } |
| 1443 | |
Rafael Espindola | 879aeb7 | 2014-12-05 16:05:19 +0000 | [diff] [blame] | 1444 | if (auto *NewGVar = dyn_cast<GlobalVariable>(NewGV)) { |
| 1445 | auto *DGVar = dyn_cast_or_null<GlobalVariable>(DGV); |
| 1446 | auto *SGVar = dyn_cast<GlobalVariable>(SGV); |
| 1447 | if (DGVar && SGVar && DGVar->isDeclaration() && SGVar->isDeclaration() && |
| 1448 | (!DGVar->isConstant() || !SGVar->isConstant())) |
| 1449 | NewGVar->setConstant(false); |
| 1450 | } |
| 1451 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1452 | if (NewGV != DGV && DGV) { |
| 1453 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType())); |
| 1454 | DGV->eraseFromParent(); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1455 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1456 | |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1457 | return ConstantExpr::getBitCast(NewGV, TypeMap.get(SGV->getType())); |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 1460 | /// Update the initializers in the Dest module now that all globals that may be |
| 1461 | /// referenced are in Dest. |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1462 | void ModuleLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) { |
| 1463 | // Figure out what the initializer looks like in the dest module. |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 1464 | Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap, |
| 1465 | RF_MoveDistinctMDs, &TypeMap, &ValMaterializer)); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 1468 | /// Copy the source function over into the dest function and fix up references |
| 1469 | /// to values. At this point we know that Dest is an external function, and |
| 1470 | /// that Src is not. |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1471 | bool ModuleLinker::linkFunctionBody(Function &Dst, Function &Src) { |
| 1472 | assert(Dst.isDeclaration() && !Src.isDeclaration()); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1473 | |
Rafael Espindola | 40d7ebe | 2014-12-08 13:29:33 +0000 | [diff] [blame] | 1474 | // Materialize if needed. |
Rafael Espindola | 3519da8 | 2014-12-08 14:25:26 +0000 | [diff] [blame] | 1475 | if (std::error_code EC = Src.materialize()) |
Rafael Espindola | 40d7ebe | 2014-12-08 13:29:33 +0000 | [diff] [blame] | 1476 | return emitError(EC.message()); |
| 1477 | |
Rafael Espindola | 869d1ce | 2014-12-08 13:44:38 +0000 | [diff] [blame] | 1478 | // Link in the prefix data. |
Rafael Espindola | 3519da8 | 2014-12-08 14:25:26 +0000 | [diff] [blame] | 1479 | if (Src.hasPrefixData()) |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 1480 | Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap, |
| 1481 | RF_MoveDistinctMDs, &TypeMap, &ValMaterializer)); |
Rafael Espindola | 869d1ce | 2014-12-08 13:44:38 +0000 | [diff] [blame] | 1482 | |
| 1483 | // Link in the prologue data. |
Rafael Espindola | 3519da8 | 2014-12-08 14:25:26 +0000 | [diff] [blame] | 1484 | if (Src.hasPrologueData()) |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 1485 | Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap, |
| 1486 | RF_MoveDistinctMDs, &TypeMap, |
| 1487 | &ValMaterializer)); |
Rafael Espindola | 869d1ce | 2014-12-08 13:44:38 +0000 | [diff] [blame] | 1488 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1489 | // Link in the personality function. |
| 1490 | if (Src.hasPersonalityFn()) |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 1491 | Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap, |
| 1492 | RF_MoveDistinctMDs, &TypeMap, |
| 1493 | &ValMaterializer)); |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | 7391dde | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 1495 | // Go through and convert function arguments over, remembering the mapping. |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1496 | Function::arg_iterator DI = Dst.arg_begin(); |
Rafael Espindola | 3519da8 | 2014-12-08 14:25:26 +0000 | [diff] [blame] | 1497 | for (Argument &Arg : Src.args()) { |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 1498 | DI->setName(Arg.getName()); // Copy the name over. |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1499 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1500 | // Add a mapping to our mapping. |
Duncan P. N. Exon Smith | 9934b26 | 2015-10-19 22:23:36 +0000 | [diff] [blame] | 1501 | ValueMap[&Arg] = &*DI; |
Rafael Espindola | a314d1a | 2014-12-08 14:20:10 +0000 | [diff] [blame] | 1502 | ++DI; |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Duncan P. N. Exon Smith | c8d987b | 2015-04-24 22:07:31 +0000 | [diff] [blame] | 1505 | // Copy over the metadata attachments. |
| 1506 | SmallVector<std::pair<unsigned, MDNode *>, 8> MDs; |
| 1507 | Src.getAllMetadata(MDs); |
| 1508 | for (const auto &I : MDs) |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 1509 | Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, RF_MoveDistinctMDs, |
| 1510 | &TypeMap, &ValMaterializer)); |
Duncan P. N. Exon Smith | c8d987b | 2015-04-24 22:07:31 +0000 | [diff] [blame] | 1511 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 1512 | // Splice the body of the source function into the dest function. |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1513 | Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1514 | |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 1515 | // At this point, all of the instructions and values of the function are now |
| 1516 | // copied over. The only problem is that they are still referencing values in |
| 1517 | // the Source function as operands. Loop through all of the operands of the |
| 1518 | // functions and patch them up to point to the local versions. |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1519 | for (BasicBlock &BB : Dst) |
Rafael Espindola | a314d1a | 2014-12-08 14:20:10 +0000 | [diff] [blame] | 1520 | for (Instruction &I : BB) |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 1521 | RemapInstruction(&I, ValueMap, |
| 1522 | RF_IgnoreMissingEntries | RF_MoveDistinctMDs, &TypeMap, |
Rafael Espindola | 9f8eff3 | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 1523 | &ValMaterializer); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1524 | |
Chris Lattner | 7391dde | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 1525 | // There is no need to map the arguments anymore. |
Rafael Espindola | 3519da8 | 2014-12-08 14:25:26 +0000 | [diff] [blame] | 1526 | for (Argument &Arg : Src.args()) |
Rafael Espindola | a314d1a | 2014-12-08 14:20:10 +0000 | [diff] [blame] | 1527 | ValueMap.erase(&Arg); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1528 | |
Eric Christopher | 97cb565 | 2015-05-15 18:20:14 +0000 | [diff] [blame] | 1529 | Src.dematerialize(); |
Rafael Espindola | 40d7ebe | 2014-12-08 13:29:33 +0000 | [diff] [blame] | 1530 | return false; |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1533 | void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) { |
| 1534 | Constant *Aliasee = Src.getAliasee(); |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 1535 | Constant *Val = MapValue(Aliasee, ValueMap, RF_MoveDistinctMDs, &TypeMap, |
| 1536 | &ValMaterializer); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1537 | Dst.setAliasee(Val); |
| 1538 | } |
| 1539 | |
Rafael Espindola | e39cd5b | 2015-12-01 22:40:40 +0000 | [diff] [blame] | 1540 | bool ModuleLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) { |
Teresa Johnson | 2d5fb8c | 2015-11-10 21:09:06 +0000 | [diff] [blame] | 1541 | if (const Comdat *SC = Src.getComdat()) { |
| 1542 | // To ensure that we don't generate an incomplete comdat group, |
| 1543 | // we must materialize and map in any other members that are not |
| 1544 | // yet materialized in Dst, which also ensures their definitions |
| 1545 | // are linked in. Otherwise, linkonce and other lazy linked GVs will |
| 1546 | // not be materialized if they aren't referenced. |
| 1547 | for (auto *SGV : ComdatMembers[SC]) { |
Rafael Espindola | 562908b | 2015-12-03 16:36:16 +0000 | [diff] [blame] | 1548 | auto *DGV = cast_or_null<GlobalValue>(ValueMap.lookup(SGV)); |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 1549 | if (DGV && !DGV->isDeclaration()) |
Teresa Johnson | 2d5fb8c | 2015-11-10 21:09:06 +0000 | [diff] [blame] | 1550 | continue; |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 1551 | MapValue(SGV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer); |
Teresa Johnson | 2d5fb8c | 2015-11-10 21:09:06 +0000 | [diff] [blame] | 1552 | } |
| 1553 | } |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 1554 | if (shouldInternalizeLinkedSymbols()) |
Rafael Espindola | e39cd5b | 2015-12-01 22:40:40 +0000 | [diff] [blame] | 1555 | if (auto *DGV = dyn_cast<GlobalValue>(&Dst)) |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 1556 | DGV->setLinkage(GlobalValue::InternalLinkage); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1557 | if (auto *F = dyn_cast<Function>(&Src)) |
Rafael Espindola | e39cd5b | 2015-12-01 22:40:40 +0000 | [diff] [blame] | 1558 | return linkFunctionBody(cast<Function>(Dst), *F); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1559 | if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) { |
Rafael Espindola | e39cd5b | 2015-12-01 22:40:40 +0000 | [diff] [blame] | 1560 | linkGlobalInit(cast<GlobalVariable>(Dst), *GVar); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1561 | return false; |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1562 | } |
Rafael Espindola | e39cd5b | 2015-12-01 22:40:40 +0000 | [diff] [blame] | 1563 | linkAliasBody(cast<GlobalAlias>(Dst), cast<GlobalAlias>(Src)); |
Rafael Espindola | ef23711 | 2014-12-08 18:45:16 +0000 | [diff] [blame] | 1564 | return false; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1565 | } |
Anton Korobeynikov | 2609888 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1566 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 1567 | /// Insert all of the named MDNodes in Src into the Dest module. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1568 | void ModuleLinker::linkNamedMDNodes() { |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1569 | const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata(); |
| 1570 | for (const NamedMDNode &NMD : SrcM.named_metadata()) { |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1571 | // Don't link module flags here. Do them separately. |
Yaron Keren | 3bf3f1f | 2015-06-15 16:20:16 +0000 | [diff] [blame] | 1572 | if (&NMD == SrcModFlags) |
| 1573 | continue; |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1574 | NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1575 | // Add Src elements into Dest node. |
Yaron Keren | 3bf3f1f | 2015-06-15 16:20:16 +0000 | [diff] [blame] | 1576 | for (const MDNode *op : NMD.operands()) |
Teresa Johnson | 83d03dd | 2015-11-15 14:50:14 +0000 | [diff] [blame] | 1577 | DestNMD->addOperand(MapMetadata( |
| 1578 | op, ValueMap, RF_MoveDistinctMDs | RF_NullMapMissingGlobalValues, |
| 1579 | &TypeMap, &ValMaterializer)); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1580 | } |
| 1581 | } |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1582 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 1583 | /// Merge the linker flags in Src into the Dest module. |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1584 | bool ModuleLinker::linkModuleFlagsMetadata() { |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1585 | // If the source module has no module flags, we are done. |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1586 | const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata(); |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 1587 | if (!SrcModFlags) |
| 1588 | return false; |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1589 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1590 | // If the destination module doesn't have module flags yet, then just copy |
| 1591 | // over the source module's flags. |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1592 | NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata(); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1593 | if (DstModFlags->getNumOperands() == 0) { |
| 1594 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) |
| 1595 | DstModFlags->addOperand(SrcModFlags->getOperand(I)); |
| 1596 | |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1600 | // First build a map of the existing module flags and requirements. |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1601 | DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags; |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 1602 | SmallSetVector<MDNode *, 16> Requirements; |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1603 | for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1604 | MDNode *Op = DstModFlags->getOperand(I); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1605 | ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0)); |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1606 | MDString *ID = cast<MDString>(Op->getOperand(1)); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1607 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1608 | if (Behavior->getZExtValue() == Module::Require) { |
| 1609 | Requirements.insert(cast<MDNode>(Op->getOperand(2))); |
| 1610 | } else { |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1611 | Flags[ID] = std::make_pair(Op, I); |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1612 | } |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1615 | // Merge in the flags from the source module, and also collect its set of |
| 1616 | // requirements. |
| 1617 | bool HasErr = false; |
| 1618 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1619 | MDNode *SrcOp = SrcModFlags->getOperand(I); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1620 | ConstantInt *SrcBehavior = |
| 1621 | mdconst::extract<ConstantInt>(SrcOp->getOperand(0)); |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1622 | MDString *ID = cast<MDString>(SrcOp->getOperand(1)); |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1623 | MDNode *DstOp; |
| 1624 | unsigned DstIndex; |
| 1625 | std::tie(DstOp, DstIndex) = Flags.lookup(ID); |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1626 | unsigned SrcBehaviorValue = SrcBehavior->getZExtValue(); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1627 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1628 | // If this is a requirement, add it and continue. |
| 1629 | if (SrcBehaviorValue == Module::Require) { |
| 1630 | // If the destination module does not already have this requirement, add |
| 1631 | // it. |
| 1632 | if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) { |
| 1633 | DstModFlags->addOperand(SrcOp); |
| 1634 | } |
| 1635 | continue; |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1638 | // If there is no existing flag with this ID, just add it. |
| 1639 | if (!DstOp) { |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1640 | Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands()); |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1641 | DstModFlags->addOperand(SrcOp); |
| 1642 | continue; |
| 1643 | } |
| 1644 | |
| 1645 | // Otherwise, perform a merge. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1646 | ConstantInt *DstBehavior = |
| 1647 | mdconst::extract<ConstantInt>(DstOp->getOperand(0)); |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1648 | unsigned DstBehaviorValue = DstBehavior->getZExtValue(); |
| 1649 | |
| 1650 | // If either flag has override behavior, handle it first. |
| 1651 | if (DstBehaviorValue == Module::Override) { |
| 1652 | // Diagnose inconsistent flags which both have override behavior. |
| 1653 | if (SrcBehaviorValue == Module::Override && |
| 1654 | SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
| 1655 | HasErr |= emitError("linking module flags '" + ID->getString() + |
| 1656 | "': IDs have conflicting override values"); |
| 1657 | } |
| 1658 | continue; |
| 1659 | } else if (SrcBehaviorValue == Module::Override) { |
| 1660 | // Update the destination flag to that of the source. |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1661 | DstModFlags->setOperand(DstIndex, SrcOp); |
| 1662 | Flags[ID].first = SrcOp; |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1663 | continue; |
| 1664 | } |
| 1665 | |
| 1666 | // Diagnose inconsistent merge behavior types. |
| 1667 | if (SrcBehaviorValue != DstBehaviorValue) { |
| 1668 | HasErr |= emitError("linking module flags '" + ID->getString() + |
| 1669 | "': IDs have conflicting behaviors"); |
| 1670 | continue; |
| 1671 | } |
| 1672 | |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1673 | auto replaceDstValue = [&](MDNode *New) { |
| 1674 | Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New}; |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1675 | MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps); |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1676 | DstModFlags->setOperand(DstIndex, Flag); |
| 1677 | Flags[ID].first = Flag; |
| 1678 | }; |
| 1679 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1680 | // Perform the merge for standard behavior types. |
| 1681 | switch (SrcBehaviorValue) { |
| 1682 | case Module::Require: |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 1683 | case Module::Override: |
| 1684 | llvm_unreachable("not possible"); |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1685 | case Module::Error: { |
| 1686 | // Emit an error if the values differ. |
| 1687 | if (SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
| 1688 | HasErr |= emitError("linking module flags '" + ID->getString() + |
| 1689 | "': IDs have conflicting values"); |
| 1690 | } |
| 1691 | continue; |
| 1692 | } |
| 1693 | case Module::Warning: { |
| 1694 | // Emit a warning if the values differ. |
| 1695 | if (SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1696 | emitWarning("linking module flags '" + ID->getString() + |
| 1697 | "': IDs have conflicting values"); |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1698 | } |
| 1699 | continue; |
| 1700 | } |
Daniel Dunbar | d77d9fb | 2013-01-16 21:38:56 +0000 | [diff] [blame] | 1701 | case Module::Append: { |
| 1702 | MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); |
| 1703 | MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1704 | SmallVector<Metadata *, 8> MDs; |
| 1705 | MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands()); |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 1706 | MDs.append(DstValue->op_begin(), DstValue->op_end()); |
| 1707 | MDs.append(SrcValue->op_begin(), SrcValue->op_end()); |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1708 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1709 | replaceDstValue(MDNode::get(DstM.getContext(), MDs)); |
Daniel Dunbar | d77d9fb | 2013-01-16 21:38:56 +0000 | [diff] [blame] | 1710 | break; |
| 1711 | } |
| 1712 | case Module::AppendUnique: { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1713 | SmallSetVector<Metadata *, 16> Elts; |
Daniel Dunbar | d77d9fb | 2013-01-16 21:38:56 +0000 | [diff] [blame] | 1714 | MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); |
| 1715 | MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 1716 | Elts.insert(DstValue->op_begin(), DstValue->op_end()); |
| 1717 | Elts.insert(SrcValue->op_begin(), SrcValue->op_end()); |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1718 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1719 | replaceDstValue(MDNode::get(DstM.getContext(), |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1720 | makeArrayRef(Elts.begin(), Elts.end()))); |
Daniel Dunbar | d77d9fb | 2013-01-16 21:38:56 +0000 | [diff] [blame] | 1721 | break; |
| 1722 | } |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1723 | } |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1726 | // Check all of the requirements. |
| 1727 | for (unsigned I = 0, E = Requirements.size(); I != E; ++I) { |
| 1728 | MDNode *Requirement = Requirements[I]; |
| 1729 | MDString *Flag = cast<MDString>(Requirement->getOperand(0)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1730 | Metadata *ReqValue = Requirement->getOperand(1); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1731 | |
Duncan P. N. Exon Smith | df55d8b | 2015-01-07 21:32:27 +0000 | [diff] [blame] | 1732 | MDNode *Op = Flags[Flag].first; |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1733 | if (!Op || Op->getOperand(2) != ReqValue) { |
| 1734 | HasErr |= emitError("linking module flags '" + Flag->getString() + |
| 1735 | "': does not have the required value"); |
| 1736 | continue; |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | return HasErr; |
| 1741 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1742 | |
Akira Hatanaka | c43df51 | 2015-02-13 00:40:41 +0000 | [diff] [blame] | 1743 | // This function returns true if the triples match. |
| 1744 | static bool triplesMatch(const Triple &T0, const Triple &T1) { |
| 1745 | // If vendor is apple, ignore the version number. |
| 1746 | if (T0.getVendor() == Triple::Apple) |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 1747 | return T0.getArch() == T1.getArch() && T0.getSubArch() == T1.getSubArch() && |
| 1748 | T0.getVendor() == T1.getVendor() && T0.getOS() == T1.getOS(); |
Akira Hatanaka | c43df51 | 2015-02-13 00:40:41 +0000 | [diff] [blame] | 1749 | |
| 1750 | return T0 == T1; |
| 1751 | } |
| 1752 | |
| 1753 | // This function returns the merged triple. |
Rafael Espindola | e3a933a | 2015-12-01 20:11:43 +0000 | [diff] [blame] | 1754 | static std::string mergeTriples(const Triple &SrcTriple, |
| 1755 | const Triple &DstTriple) { |
Akira Hatanaka | c43df51 | 2015-02-13 00:40:41 +0000 | [diff] [blame] | 1756 | // If vendor is apple, pick the triple with the larger version number. |
| 1757 | if (SrcTriple.getVendor() == Triple::Apple) |
| 1758 | if (DstTriple.isOSVersionLT(SrcTriple)) |
| 1759 | return SrcTriple.str(); |
| 1760 | |
| 1761 | return DstTriple.str(); |
| 1762 | } |
| 1763 | |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1764 | bool ModuleLinker::linkIfNeeded(GlobalValue &GV) { |
| 1765 | GlobalValue *DGV = getLinkedToGlobal(&GV); |
| 1766 | |
| 1767 | if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration())) |
| 1768 | return false; |
| 1769 | |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 1770 | if (DGV && !GV.hasLocalLinkage() && !GV.hasAppendingLinkage()) { |
| 1771 | auto *DGVar = dyn_cast<GlobalVariable>(DGV); |
| 1772 | auto *SGVar = dyn_cast<GlobalVariable>(&GV); |
| 1773 | if (DGVar && SGVar) { |
| 1774 | if (DGVar->isDeclaration() && SGVar->isDeclaration() && |
| 1775 | (!DGVar->isConstant() || !SGVar->isConstant())) { |
| 1776 | DGVar->setConstant(false); |
| 1777 | SGVar->setConstant(false); |
| 1778 | } |
| 1779 | if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) { |
| 1780 | unsigned Align = std::max(DGVar->getAlignment(), SGVar->getAlignment()); |
| 1781 | SGVar->setAlignment(Align); |
| 1782 | DGVar->setAlignment(Align); |
| 1783 | } |
| 1784 | } |
| 1785 | |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1786 | GlobalValue::VisibilityTypes Visibility = |
| 1787 | getMinVisibility(DGV->getVisibility(), GV.getVisibility()); |
| 1788 | DGV->setVisibility(Visibility); |
| 1789 | GV.setVisibility(Visibility); |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 1790 | |
| 1791 | bool HasUnnamedAddr = GV.hasUnnamedAddr() && DGV->hasUnnamedAddr(); |
| 1792 | DGV->setUnnamedAddr(HasUnnamedAddr); |
| 1793 | GV.setUnnamedAddr(HasUnnamedAddr); |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 1796 | // Don't want to append to global_ctors list, for example, when we |
| 1797 | // are importing for ThinLTO, otherwise the global ctors and dtors |
| 1798 | // get executed multiple times for local variables (the latter causing |
| 1799 | // double frees). |
| 1800 | if (GV.hasAppendingLinkage() && isPerformingImport()) |
| 1801 | return false; |
| 1802 | |
| 1803 | if (isPerformingImport() && !doImportAsDefinition(&GV)) |
| 1804 | return false; |
| 1805 | |
| 1806 | if (!DGV && !shouldOverrideFromSrc() && |
| 1807 | (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() || |
| 1808 | GV.hasAvailableExternallyLinkage())) |
| 1809 | return false; |
| 1810 | |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1811 | if (const Comdat *SC = GV.getComdat()) { |
| 1812 | bool LinkFromSrc; |
| 1813 | Comdat::SelectionKind SK; |
| 1814 | std::tie(SK, LinkFromSrc) = ComdatsChosen[SC]; |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 1815 | if (LinkFromSrc) |
| 1816 | ValuesToLink.insert(&GV); |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1817 | return false; |
| 1818 | } |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 1819 | |
| 1820 | bool LinkFromSrc = true; |
| 1821 | if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV)) |
| 1822 | return true; |
| 1823 | if (LinkFromSrc) |
| 1824 | ValuesToLink.insert(&GV); |
| 1825 | return false; |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1828 | bool ModuleLinker::run() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1829 | // Inherit the target data from the source module if the destination module |
| 1830 | // doesn't have one already. |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1831 | if (DstM.getDataLayout().isDefault()) |
| 1832 | DstM.setDataLayout(SrcM.getDataLayout()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1833 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1834 | if (SrcM.getDataLayout() != DstM.getDataLayout()) { |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1835 | emitWarning("Linking two modules of different data layouts: '" + |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1836 | SrcM.getModuleIdentifier() + "' is '" + |
| 1837 | SrcM.getDataLayoutStr() + "' whereas '" + |
| 1838 | DstM.getModuleIdentifier() + "' is '" + |
| 1839 | DstM.getDataLayoutStr() + "'\n"); |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1840 | } |
Akira Hatanaka | c43df51 | 2015-02-13 00:40:41 +0000 | [diff] [blame] | 1841 | |
| 1842 | // Copy the target triple from the source to dest if the dest's is empty. |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1843 | if (DstM.getTargetTriple().empty() && !SrcM.getTargetTriple().empty()) |
| 1844 | DstM.setTargetTriple(SrcM.getTargetTriple()); |
Akira Hatanaka | c43df51 | 2015-02-13 00:40:41 +0000 | [diff] [blame] | 1845 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1846 | Triple SrcTriple(SrcM.getTargetTriple()), DstTriple(DstM.getTargetTriple()); |
Akira Hatanaka | c43df51 | 2015-02-13 00:40:41 +0000 | [diff] [blame] | 1847 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1848 | if (!SrcM.getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple)) |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1849 | emitWarning("Linking two modules of different target triples: " + |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1850 | SrcM.getModuleIdentifier() + "' is '" + SrcM.getTargetTriple() + |
| 1851 | "' whereas '" + DstM.getModuleIdentifier() + "' is '" + |
| 1852 | DstM.getTargetTriple() + "'\n"); |
Akira Hatanaka | c43df51 | 2015-02-13 00:40:41 +0000 | [diff] [blame] | 1853 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1854 | DstM.setTargetTriple(mergeTriples(SrcTriple, DstTriple)); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1855 | |
| 1856 | // Append the module inline asm string. |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1857 | if (!SrcM.getModuleInlineAsm().empty()) { |
| 1858 | if (DstM.getModuleInlineAsm().empty()) |
| 1859 | DstM.setModuleInlineAsm(SrcM.getModuleInlineAsm()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1860 | else |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1861 | DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" + |
| 1862 | SrcM.getModuleInlineAsm()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1865 | // Loop over all of the linked values to compute type mappings. |
| 1866 | computeTypeMapping(); |
| 1867 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1868 | ComdatsChosen.clear(); |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1869 | for (const auto &SMEC : SrcM.getComdatSymbolTable()) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1870 | const Comdat &C = SMEC.getValue(); |
| 1871 | if (ComdatsChosen.count(&C)) |
| 1872 | continue; |
| 1873 | Comdat::SelectionKind SK; |
| 1874 | bool LinkFromSrc; |
| 1875 | if (getComdatResult(&C, SK, LinkFromSrc)) |
| 1876 | return true; |
| 1877 | ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc); |
| 1878 | } |
| 1879 | |
Duncan P. N. Exon Smith | 09d84ad | 2014-08-12 16:46:37 +0000 | [diff] [blame] | 1880 | // Upgrade mismatched global arrays. |
| 1881 | upgradeMismatchedGlobals(); |
| 1882 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1883 | for (GlobalVariable &GV : SrcM.globals()) |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1884 | if (const Comdat *SC = GV.getComdat()) |
| 1885 | ComdatMembers[SC].push_back(&GV); |
| 1886 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1887 | for (Function &SF : SrcM) |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1888 | if (const Comdat *SC = SF.getComdat()) |
| 1889 | ComdatMembers[SC].push_back(&SF); |
| 1890 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1891 | for (GlobalAlias &GA : SrcM.aliases()) |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1892 | if (const Comdat *SC = GA.getComdat()) |
| 1893 | ComdatMembers[SC].push_back(&GA); |
| 1894 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1895 | // Insert all of the globals in src into the DstM module... without linking |
| 1896 | // initializers (which could refer to functions not yet mapped over). |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1897 | for (GlobalVariable &GV : SrcM.globals()) |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1898 | if (linkIfNeeded(GV)) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1899 | return true; |
| 1900 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1901 | for (Function &SF : SrcM) |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1902 | if (linkIfNeeded(SF)) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1903 | return true; |
| 1904 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1905 | for (GlobalAlias &GA : SrcM.aliases()) |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 1906 | if (linkIfNeeded(GA)) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1907 | return true; |
| 1908 | |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 1909 | for (GlobalValue *GV : ValuesToLink) { |
| 1910 | MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer); |
| 1911 | if (HasError) |
| 1912 | return true; |
Rafael Espindola | beadd56 | 2014-12-08 18:05:48 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Teresa Johnson | 1063293 | 2015-11-06 17:50:53 +0000 | [diff] [blame] | 1915 | // Note that we are done linking global value bodies. This prevents |
| 1916 | // metadata linking from creating new references. |
| 1917 | DoneLinkingBodies = true; |
| 1918 | |
Teresa Johnson | 189b252 | 2015-11-06 17:50:48 +0000 | [diff] [blame] | 1919 | // Remap all of the named MDNodes in Src into the DstM module. We do this |
| 1920 | // after linking GlobalValues so that MDNodes that reference GlobalValues |
| 1921 | // are properly remapped. |
| 1922 | linkNamedMDNodes(); |
| 1923 | |
| 1924 | // Merge the module flags into the DstM module. |
| 1925 | if (linkModuleFlagsMetadata()) |
| 1926 | return true; |
| 1927 | |
Anton Korobeynikov | 2609888 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1928 | return false; |
| 1929 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1930 | |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 1931 | Linker::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P) |
| 1932 | : ETypes(E), IsPacked(P) {} |
| 1933 | |
| 1934 | Linker::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST) |
| 1935 | : ETypes(ST->elements()), IsPacked(ST->isPacked()) {} |
| 1936 | |
| 1937 | bool Linker::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const { |
| 1938 | if (IsPacked != That.IsPacked) |
| 1939 | return false; |
| 1940 | if (ETypes != That.ETypes) |
| 1941 | return false; |
| 1942 | return true; |
| 1943 | } |
| 1944 | |
| 1945 | bool Linker::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const { |
| 1946 | return !this->operator==(That); |
| 1947 | } |
| 1948 | |
| 1949 | StructType *Linker::StructTypeKeyInfo::getEmptyKey() { |
| 1950 | return DenseMapInfo<StructType *>::getEmptyKey(); |
| 1951 | } |
| 1952 | |
| 1953 | StructType *Linker::StructTypeKeyInfo::getTombstoneKey() { |
| 1954 | return DenseMapInfo<StructType *>::getTombstoneKey(); |
| 1955 | } |
| 1956 | |
| 1957 | unsigned Linker::StructTypeKeyInfo::getHashValue(const KeyTy &Key) { |
| 1958 | return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()), |
| 1959 | Key.IsPacked); |
| 1960 | } |
| 1961 | |
| 1962 | unsigned Linker::StructTypeKeyInfo::getHashValue(const StructType *ST) { |
| 1963 | return getHashValue(KeyTy(ST)); |
| 1964 | } |
| 1965 | |
| 1966 | bool Linker::StructTypeKeyInfo::isEqual(const KeyTy &LHS, |
| 1967 | const StructType *RHS) { |
| 1968 | if (RHS == getEmptyKey() || RHS == getTombstoneKey()) |
| 1969 | return false; |
| 1970 | return LHS == KeyTy(RHS); |
| 1971 | } |
| 1972 | |
| 1973 | bool Linker::StructTypeKeyInfo::isEqual(const StructType *LHS, |
| 1974 | const StructType *RHS) { |
| 1975 | if (RHS == getEmptyKey()) |
| 1976 | return LHS == getEmptyKey(); |
| 1977 | |
| 1978 | if (RHS == getTombstoneKey()) |
| 1979 | return LHS == getTombstoneKey(); |
| 1980 | |
| 1981 | return KeyTy(LHS) == KeyTy(RHS); |
| 1982 | } |
| 1983 | |
| 1984 | void Linker::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) { |
| 1985 | assert(!Ty->isOpaque()); |
Benjamin Kramer | 3280a5d | 2014-12-06 19:22:54 +0000 | [diff] [blame] | 1986 | NonOpaqueStructTypes.insert(Ty); |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Rafael Espindola | a5b9e1c | 2015-03-06 00:50:21 +0000 | [diff] [blame] | 1989 | void Linker::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) { |
| 1990 | assert(!Ty->isOpaque()); |
| 1991 | NonOpaqueStructTypes.insert(Ty); |
| 1992 | bool Removed = OpaqueStructTypes.erase(Ty); |
| 1993 | (void)Removed; |
| 1994 | assert(Removed); |
| 1995 | } |
| 1996 | |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 1997 | void Linker::IdentifiedStructTypeSet::addOpaque(StructType *Ty) { |
| 1998 | assert(Ty->isOpaque()); |
| 1999 | OpaqueStructTypes.insert(Ty); |
| 2000 | } |
| 2001 | |
| 2002 | StructType * |
| 2003 | Linker::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes, |
| 2004 | bool IsPacked) { |
| 2005 | Linker::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked); |
| 2006 | auto I = NonOpaqueStructTypes.find_as(Key); |
| 2007 | if (I == NonOpaqueStructTypes.end()) |
| 2008 | return nullptr; |
Benjamin Kramer | 3280a5d | 2014-12-06 19:22:54 +0000 | [diff] [blame] | 2009 | return *I; |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | bool Linker::IdentifiedStructTypeSet::hasType(StructType *Ty) { |
| 2013 | if (Ty->isOpaque()) |
| 2014 | return OpaqueStructTypes.count(Ty); |
| 2015 | auto I = NonOpaqueStructTypes.find(Ty); |
| 2016 | if (I == NonOpaqueStructTypes.end()) |
| 2017 | return false; |
Benjamin Kramer | 3280a5d | 2014-12-06 19:22:54 +0000 | [diff] [blame] | 2018 | return *I == Ty; |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 2021 | Linker::Linker(Module &M, DiagnosticHandlerFunction DiagnosticHandler) |
| 2022 | : Composite(M), DiagnosticHandler(DiagnosticHandler) { |
Rafael Espindola | a4e85e3 | 2014-12-01 16:32:20 +0000 | [diff] [blame] | 2023 | TypeFinder StructTypes; |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 2024 | StructTypes.run(M, true); |
Rafael Espindola | 31ad468 | 2014-12-03 22:36:37 +0000 | [diff] [blame] | 2025 | for (StructType *Ty : StructTypes) { |
| 2026 | if (Ty->isOpaque()) |
| 2027 | IdentifiedStructTypes.addOpaque(Ty); |
| 2028 | else |
| 2029 | IdentifiedStructTypes.addNonOpaque(Ty); |
| 2030 | } |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 2031 | } |
Rafael Espindola | 3df61b7 | 2013-05-04 03:48:37 +0000 | [diff] [blame] | 2032 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 2033 | bool Linker::linkInModule(Module &Src, unsigned Flags, |
Mehdi Amini | 8220e8a | 2015-11-23 01:59:16 +0000 | [diff] [blame] | 2034 | const FunctionInfoIndex *Index, |
Mehdi Amini | 7471cf8 | 2015-12-03 02:37:30 +0000 | [diff] [blame] | 2035 | DenseSet<const GlobalValue *> *FunctionsToImport) { |
Rafael Espindola | a4e85e3 | 2014-12-01 16:32:20 +0000 | [diff] [blame] | 2036 | ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, |
Mehdi Amini | 7471cf8 | 2015-12-03 02:37:30 +0000 | [diff] [blame] | 2037 | DiagnosticHandler, Flags, Index, FunctionsToImport); |
Manman Ren | dab999d | 2015-01-20 19:24:59 +0000 | [diff] [blame] | 2038 | bool RetCode = TheLinker.run(); |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 2039 | Composite.dropTriviallyDeadConstantArrays(); |
Manman Ren | dab999d | 2015-01-20 19:24:59 +0000 | [diff] [blame] | 2040 | return RetCode; |
Rafael Espindola | 3df61b7 | 2013-05-04 03:48:37 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2043 | //===----------------------------------------------------------------------===// |
| 2044 | // LinkModules entrypoint. |
| 2045 | //===----------------------------------------------------------------------===// |
| 2046 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 2047 | /// This function links two modules together, with the resulting Dest module |
| 2048 | /// modified to be the composite of the two input modules. If an error occurs, |
| 2049 | /// true is returned and ErrorMsg (if not null) is set to indicate the problem. |
| 2050 | /// Upon failure, the Dest module could be in a modified state, and shouldn't be |
| 2051 | /// relied on to be consistent. |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 2052 | bool Linker::linkModules(Module &Dest, Module &Src, |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 2053 | DiagnosticHandlerFunction DiagnosticHandler, |
| 2054 | unsigned Flags) { |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 2055 | Linker L(Dest, DiagnosticHandler); |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 2056 | return L.linkInModule(Src, Flags); |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Bill Wendling | a3aeb98 | 2012-05-09 08:55:40 +0000 | [diff] [blame] | 2059 | //===----------------------------------------------------------------------===// |
| 2060 | // C API. |
| 2061 | //===----------------------------------------------------------------------===// |
| 2062 | |
| 2063 | LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src, |
Juergen Ributzka | a57d588 | 2015-03-02 18:59:38 +0000 | [diff] [blame] | 2064 | LLVMLinkerMode Unused, char **OutMessages) { |
Rafael Espindola | 98ab63c | 2014-10-25 04:31:08 +0000 | [diff] [blame] | 2065 | Module *D = unwrap(Dest); |
Rafael Espindola | 98ab63c | 2014-10-25 04:31:08 +0000 | [diff] [blame] | 2066 | std::string Message; |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 2067 | raw_string_ostream Stream(Message); |
| 2068 | DiagnosticPrinterRawOStream DP(Stream); |
| 2069 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 2070 | LLVMBool Result = Linker::linkModules( |
| 2071 | *D, *unwrap(Src), [&](const DiagnosticInfo &DI) { DI.print(DP); }); |
Rafael Espindola | 98ab63c | 2014-10-25 04:31:08 +0000 | [diff] [blame] | 2072 | |
Eli Bendersky | ff715e2 | 2015-06-12 23:26:42 +0000 | [diff] [blame] | 2073 | if (OutMessages && Result) { |
| 2074 | Stream.flush(); |
Rafael Espindola | 98ab63c | 2014-10-25 04:31:08 +0000 | [diff] [blame] | 2075 | *OutMessages = strdup(Message.c_str()); |
Eli Bendersky | ff715e2 | 2015-06-12 23:26:42 +0000 | [diff] [blame] | 2076 | } |
Bill Wendling | a3aeb98 | 2012-05-09 08:55:40 +0000 | [diff] [blame] | 2077 | return Result; |
| 2078 | } |