55 TypeMapTy(TypeSet &Set) : DstStructTypesSet(Set) {}
57 TypeSet &DstStructTypesSet;
61 void addTypeMapping(
Type *DstTy,
Type *SrcTy);
65 void linkDefinedTypeBodies();
76 I = MappedTypes.begin(), E = MappedTypes.end();
I != E; ++
I) {
77 dbgs() <<
"TypeMap: ";
92 bool areTypesIsomorphic(
Type *DstTy,
Type *SrcTy);
96 void TypeMapTy::addTypeMapping(
Type *DstTy,
Type *SrcTy) {
97 Type *&Entry = MappedTypes[SrcTy];
100 if (DstTy == SrcTy) {
107 if (!areTypesIsomorphic(DstTy, SrcTy)) {
110 for (
unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i)
111 MappedTypes.erase(SpeculativeTypes[i]);
113 SpeculativeTypes.clear();
118 bool TypeMapTy::areTypesIsomorphic(
Type *DstTy,
Type *SrcTy) {
123 Type *&Entry = MappedTypes[SrcTy];
125 return Entry == DstTy;
129 if (DstTy == SrcTy) {
137 if (
StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
139 if (SSTy->isOpaque()) {
141 SpeculativeTypes.push_back(SrcTy);
150 if (cast<StructType>(DstTy)->isOpaque()) {
152 if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)))
154 SrcDefinitionsToResolve.push_back(SSTy);
165 if (isa<IntegerType>(DstTy))
167 if (
PointerType *PT = dyn_cast<PointerType>(DstTy)) {
168 if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
171 }
else if (
FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
172 if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
174 }
else if (
StructType *DSTy = dyn_cast<StructType>(DstTy)) {
176 if (DSTy->isLiteral() != SSTy->
isLiteral() ||
177 DSTy->isPacked() != SSTy->
isPacked())
179 }
else if (
ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
180 if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
182 }
else if (
VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
183 if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
190 SpeculativeTypes.push_back(SrcTy);
203 void TypeMapTy::linkDefinedTypeBodies() {
209 while (!SrcDefinitionsToResolve.empty()) {
210 StructType *SrcSTy = SrcDefinitionsToResolve.pop_back_val();
211 StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
217 assert(!SrcSTy->
isOpaque() &&
"Not resolving a definition?");
221 for (
unsigned i = 0, e = Elements.
size(); i != e; ++i)
228 if (!SrcSTy->
hasName())
continue;
239 DstResolvedOpaqueTypes.clear();
245 Type *Result = getImpl(Ty);
248 if (!SrcDefinitionsToResolve.empty())
249 linkDefinedTypeBodies();
254 Type *TypeMapTy::getImpl(
Type *Ty) {
256 Type **Entry = &MappedTypes[Ty];
257 if (*Entry)
return *Entry;
261 if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) {
268 bool AnyChange =
false;
277 Entry = &MappedTypes[Ty];
278 if (*Entry)
return *Entry;
290 cast<ArrayType>(Ty)->getNumElements());
293 cast<VectorType>(Ty)->getNumElements());
296 cast<PointerType>(Ty)->getAddressSpace());
300 cast<FunctionType>(Ty)->isVarArg());
304 cast<StructType>(Ty)->isPacked());
337 DstStructTypesSet.insert(STy);
343 SrcDefinitionsToResolve.push_back(STy);
347 DstStructTypesSet.insert(DTy);
348 DstResolvedOpaqueTypes.insert(DTy);
365 std::vector<Function*> &LazilyLinkFunctions;
367 ValueMaterializerTy(TypeMapTy &TypeMap,
Module *DstM,
368 std::vector<Function*> &LazilyLinkFunctions) :
370 LazilyLinkFunctions(LazilyLinkFunctions) {
382 ValueMaterializerTy ValMaterializer;
390 struct AppendingVarInfo {
396 std::vector<AppendingVarInfo> AppendingVars;
404 std::vector<Function*> LazilyLinkFunctions;
407 std::string ErrorMsg;
409 ModuleLinker(
Module *dstM, TypeSet &Set,
Module *srcM,
unsigned mode)
410 : DstM(dstM), SrcM(srcM), TypeMap(Set),
411 ValMaterializer(TypeMap, DstM, LazilyLinkFunctions),
419 bool emitError(
const Twine &Message) {
420 ErrorMsg = Message.
str();
441 if (DGV == 0)
return 0;
452 void computeTypeMapping();
456 bool linkFunctionProto(
Function *SrcF);
458 bool linkModuleFlagsMetadata();
460 void linkAppendingVarInit(
const AppendingVarInfo &AVI);
461 void linkGlobalInits();
463 void linkAliasBodies();
464 void linkNamedMDNodes();
482 ConflictGV->setName(Name);
483 assert(ConflictGV->getName() != Name &&
"forceRenaming didn't work");
513 Value *ValueMaterializerTy::materializeValueFor(
Value *V) {
522 LazilyLinkFunctions.push_back(SF);
536 assert(Dest &&
"Must have two globals being queried");
538 "If Src has internal linkage, Dest shouldn't be set!");
543 if (SrcIsDeclaration) {
548 if (DestIsDeclaration) {
591 "Unexpected linkage type!");
592 return emitError(
"Linking globals named '" + Src->
getName() +
593 "': symbol multiply defined!");
607 void ModuleLinker::computeTypeMapping() {
610 E = SrcM->global_end();
I != E; ++
I) {
612 if (DGV == 0)
continue;
615 TypeMap.addTypeMapping(DGV->
getType(),
I->getType());
621 ArrayType *SAT = cast<ArrayType>(
I->getType()->getElementType());
622 TypeMap.addTypeMapping(DAT->
getElementType(), SAT->getElementType());
628 TypeMap.addTypeMapping(DGV->
getType(),
I->getType());
636 SrcStructTypes.
run(*SrcM,
true);
638 SrcStructTypes.
end());
640 for (
unsigned i = 0, e = SrcStructTypes.
size(); i != e; ++i) {
670 if (!SrcStructTypesSet.count(DST) && TypeMap.DstStructTypesSet.count(DST))
671 TypeMap.addTypeMapping(DST, ST);
678 TypeMap.linkDefinedTypeBodies();
687 return emitError(
"Linking globals named '" + SrcGV->
getName() +
688 "': can only link appending global with another appending global!");
696 if (EltTy != SrcTy->getElementType())
697 return emitError(
"Appending variables with different element types!");
699 return emitError(
"Appending variables linked with different const'ness!");
703 "Appending variables with different alignment need to be linked!");
707 "Appending variables with different visibility need to be linked!");
711 "Appending variables with different unnamed_addr need to be linked!");
715 "Appending variables with different section name need to be linked!");
717 uint64_t NewSize = DstTy->
getNumElements() + SrcTy->getNumElements();
730 AppendingVarInfo AVI;
734 AppendingVars.push_back(AVI);
744 DoNotLinkFromSource.insert(SrcGV);
759 return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV);
765 bool LinkFromSrc =
false;
766 if (getLinkageResult(DGV, SGV, NewLinkage, NV, LinkFromSrc))
776 if (DGVar->isDeclaration() && SGV->
isConstant() && !DGVar->isConstant())
777 DGVar->setConstant(
true);
789 DoNotLinkFromSource.insert(SGV);
822 bool ModuleLinker::linkFunctionProto(
Function *SF) {
829 bool LinkFromSrc =
false;
831 if (getLinkageResult(DGV, SF, NewLinkage, NV, LinkFromSrc))
847 DoNotLinkFromSource.insert(SF);
857 DoNotLinkFromSource.insert(SF);
882 bool ModuleLinker::linkAliasProto(
GlobalAlias *SGA) {
889 bool LinkFromSrc =
false;
890 if (getLinkageResult(DGV, SGA, NewLinkage, NV, LinkFromSrc))
903 DoNotLinkFromSource.insert(SGA);
929 unsigned NumElements = cast<ArrayType>(C->
getType())->getNumElements();
931 for (
unsigned i = 0; i != NumElements; ++i)
935 void ModuleLinker::linkAppendingVarInit(
const AppendingVarInfo &AVI) {
943 ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
949 void ModuleLinker::linkGlobalInits() {
952 E = SrcM->global_end();
I != E; ++
I) {
955 if (!
I->hasInitializer() || DoNotLinkFromSource.count(
I))
continue;
961 RF_None, &TypeMap, &ValMaterializer));
975 DI->setName(
I->getName());
992 &TypeMap, &ValMaterializer);
998 &TypeMap, &ValMaterializer);
1009 void ModuleLinker::linkAliasBodies() {
1012 if (DoNotLinkFromSource.count(
I))
1014 if (
Constant *Aliasee =
I->getAliasee()) {
1017 &TypeMap, &ValMaterializer));
1024 void ModuleLinker::linkNamedMDNodes() {
1025 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1027 E = SrcM->named_metadata_end();
I != E; ++
I) {
1029 if (&*
I == SrcModFlags)
continue;
1030 NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(
I->getName());
1032 for (
unsigned i = 0, e =
I->getNumOperands(); i != e; ++i)
1034 RF_None, &TypeMap, &ValMaterializer));
1040 bool ModuleLinker::linkModuleFlagsMetadata() {
1042 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1043 if (!SrcModFlags)
return false;
1047 NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
1072 bool HasErr =
false;
1078 unsigned SrcBehaviorValue = SrcBehavior->
getZExtValue();
1098 ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0));
1099 unsigned DstBehaviorValue = DstBehavior->
getZExtValue();
1105 SrcOp->
getOperand(2) != DstOp->getOperand(2)) {
1106 HasErr |= emitError(
"linking module flags '" + ID->getString() +
1107 "': IDs have conflicting override values");
1112 DstOp->replaceOperandWith(0, SrcBehavior);
1113 DstOp->replaceOperandWith(2, SrcOp->
getOperand(2));
1118 if (SrcBehaviorValue != DstBehaviorValue) {
1119 HasErr |= emitError(
"linking module flags '" + ID->getString() +
1120 "': IDs have conflicting behaviors");
1125 switch (SrcBehaviorValue) {
1130 if (SrcOp->
getOperand(2) != DstOp->getOperand(2)) {
1131 HasErr |= emitError(
"linking module flags '" + ID->getString() +
1132 "': IDs have conflicting values");
1138 if (SrcOp->
getOperand(2) != DstOp->getOperand(2)) {
1139 errs() <<
"WARNING: linking module flags '" << ID->getString()
1140 <<
"': IDs have conflicting values";
1145 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1147 unsigned NumOps = DstValue->
getNumOperands() + SrcValue->getNumOperands();
1148 Value **VP, **Values = VP =
new Value*[NumOps];
1149 for (
unsigned i = 0, e = DstValue->
getNumOperands(); i != e; ++i, ++VP)
1151 for (
unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP)
1152 *VP = SrcValue->getOperand(i);
1153 DstOp->replaceOperandWith(2,
MDNode::get(DstM->getContext(),
1161 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1163 for (
unsigned i = 0, e = DstValue->
getNumOperands(); i != e; ++i)
1165 for (
unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
1166 Elts.
insert(SrcValue->getOperand(i));
1167 DstOp->replaceOperandWith(2,
MDNode::get(DstM->getContext(),
1176 for (
unsigned I = 0, E = Requirements.
size();
I != E; ++
I) {
1177 MDNode *Requirement = Requirements[
I];
1181 MDNode *Op = Flags[Flag];
1183 HasErr |= emitError(
"linking module flags '" + Flag->
getString() +
1184 "': does not have the required value");
1192 bool ModuleLinker::run() {
1193 assert(DstM &&
"Null destination module");
1194 assert(SrcM &&
"Null source module");
1198 if (DstM->getDataLayout().empty() && !SrcM->getDataLayout().empty())
1199 DstM->setDataLayout(SrcM->getDataLayout());
1202 if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1203 DstM->setTargetTriple(SrcM->getTargetTriple());
1205 if (!SrcM->getDataLayout().empty() && !DstM->getDataLayout().empty() &&
1206 SrcM->getDataLayout() != DstM->getDataLayout())
1207 errs() <<
"WARNING: Linking two modules of different data layouts!\n";
1208 if (!SrcM->getTargetTriple().empty() &&
1209 DstM->getTargetTriple() != SrcM->getTargetTriple()) {
1210 errs() <<
"WARNING: Linking two modules of different target triples: ";
1211 if (!SrcM->getModuleIdentifier().empty())
1212 errs() << SrcM->getModuleIdentifier() <<
": ";
1213 errs() <<
"'" << SrcM->getTargetTriple() <<
"' and '"
1214 << DstM->getTargetTriple() <<
"'\n";
1218 if (!SrcM->getModuleInlineAsm().empty()) {
1219 if (DstM->getModuleInlineAsm().empty())
1220 DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
1222 DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+
"\n"+
1223 SrcM->getModuleInlineAsm());
1227 computeTypeMapping();
1232 E = SrcM->global_end();
I != E; ++
I)
1233 if (linkGlobalProto(
I))
1242 if (linkFunctionProto(
I))
1247 E = SrcM->alias_end();
I != E; ++
I)
1248 if (linkAliasProto(
I))
1251 for (
unsigned i = 0, e = AppendingVars.size(); i != e; ++i)
1252 linkAppendingVarInit(AppendingVars[i]);
1262 if (DoNotLinkFromSource.count(SF))
continue;
1279 linkFunctionBody(DF, SF);
1292 if (linkModuleFlagsMetadata())
1296 bool LinkedInAnyFunctions;
1298 LinkedInAnyFunctions =
false;
1300 for(std::vector<Function*>::iterator
I = LazilyLinkFunctions.begin(),
1301 E = LazilyLinkFunctions.end();
I != E; ++
I) {
1326 LazilyLinkFunctions.erase(
I);
1329 linkFunctionBody(DF, SF);
1334 LinkedInAnyFunctions =
true;
1337 }
while (LinkedInAnyFunctions);
1341 TypeMap.linkDefinedTypeBodies();
1348 StructTypes.
run(*M,
true);
1349 IdentifiedStructTypes.insert(StructTypes.
begin(), StructTypes.
end());
1361 ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, Mode);
1362 if (TheLinker.run()) {
1364 *ErrorMsg = TheLinker.ErrorMsg;
1380 std::string *ErrorMsg) {
1391 std::string Messages;
1393 Mode, OutMessages? &Messages : 0);
1395 *OutMessages =
strdup(Messages.c_str());
void setVisibility(VisibilityTypes V)
void push_back(const T &Elt)
LinkageTypes getLinkage() const
StringRef getString() const
VisibilityTypes getVisibility() const
virtual void copyAttributesFrom(const GlobalValue *Src)
ThreadLocalMode getThreadLocalMode() const
size_t size() const
size - Get the string size.
The main container class for the LLVM Intermediate Representation.
unsigned getAlignment() const
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getNumOperands() const
getNumOperands - Return number of MDNode operands.
void addOperand(MDNode *M)
addOperand - Add metadata operand.
bool hasAppendingLinkage() const
static PointerType * get(Type *ElementType, unsigned AddressSpace)
size_t rfind(char C, size_t From=npos) const
StringRef substr(size_t Start, size_t N=npos) const
Externally visible function.
iterator insert(iterator I, const T &Elt)
bool hasAvailableExternallyLinkage() const
MDNode - a tuple of other values.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
LLVMContext ** unwrap(LLVMContextRef *Tys)
static bool isLessConstraining(GlobalValue::VisibilityTypes a, GlobalValue::VisibilityTypes b)
const Constant * getInitializer() const
iterator end()
Get an iterator to the end of the SetVector.
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
size_type size() const
Determine the number of elements in the SetVector.
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=0, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
static MDNode * get(LLVMContext &Context, ArrayRef< Value * > Vals)
StringRef getName() const
Value * getOperand(unsigned i) const LLVM_READONLY
getOperand - Return specified operand.
Appends the two values, which are required to be metadata nodes.
std::string str() const
str - Return the twine contents as a std::string.
bool hasCommonLinkage() const
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
void setInitializer(Constant *InitVal)
#define llvm_unreachable(msg)
void setName(const Twine &Name)
ID
LLVM Calling Convention Representation.
static void copyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV)
uint64_t getZExtValue() const
Return the zero extended value.
bool insert(const value_type &X)
Insert a new element into the SetVector.
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
VisibilityTypes
An enumeration for the kinds of visibility of global values.
bool isMaterializable() const
iterator begin()
Get an iterator to the beginning of the SetVector.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
static void forceRenaming(GlobalValue *GV, StringRef Name)
void replaceAllUsesWith(Value *V)
Type * getElementType() const
static bool LinkModules(Module *Dest, Module *Src, unsigned Mode, std::string *ErrorMsg)
static bool isWeakForLinker(LinkageTypes Linkage)
static void getArrayElements(Constant *C, SmallVectorImpl< Constant * > &Dest)
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
Type * getContainedType(unsigned i) const
Type * getElementType(unsigned N) const
LLVM Constant Representation.
LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src, LLVMLinkerMode Mode, char **OutMessages)
bool hasDLLExportLinkage() const
char back() const
back - Get the last character in the string.
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
virtual void eraseFromParent()
uint64_t getNumElements() const
MDNode * getOperand(unsigned i) const
getOperand - Return specified operand.
Constant * getAggregateElement(unsigned Elt) const
unsigned getNumContainedTypes() const
const std::string & getSection() const
bool hasWeakLinkage() const
void setBody(ArrayRef< Type * > Elements, bool isPacked=false)
setBody - Specify a body for an opaque identified type.
char *strdup(const char *s1);
bool hasExternalWeakLinkage() const
bool hasExternalLinkage() const
static Constant * getBitCast(Constant *C, Type *Ty)
A SetVector that performs no allocations if smaller than a certain size.
const BasicBlockListType & getBasicBlockList() const
Class for constant integers.
15: SIMD 'packed' format, or other vector type
void run(const Module &M, bool onlyNamed)
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
void setAlignment(unsigned Align)
bool linkInModule(Module *Src, unsigned Mode, std::string *ErrorMsg)
Link Src into the composite. The source is destroyed if Mode is DestroySource and preserved if it is ...
bool hasName() const
hasName - Return true if this is a named struct that has a non-empty name.
void setUnnamedAddr(bool Val)
void setAliasee(Constant *GV)
set/getAliasee - These methods retrive and set alias target.
void setLinkage(LinkageTypes LT)
void splice(iterator where, iplist &L2)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
StringRef str() const
Explicit conversion to StringRef.
LinkageTypes
An enumeration for the kinds of linkage for global values.
StringRef getName() const
bool hasLinkOnceLinkage() const
void setName(StringRef Name)
PointerType * getType() const
getType - Global values are always pointers.
bool isDeclaration() const
FunctionType * getFunctionType() const
static ArrayType * get(Type *ElementType, uint64_t NumElements)
bool hasPrefixData() const
Rename collisions when linking (static functions).
Constant * getPrefixData() const
bool hasLocalLinkage() const
struct LLVMOpaqueModule * LLVMModuleRef
LLVM Value Representation.
bool hasUnnamedAddr() const
ValueT lookup(const KeyT &Val) const
static VectorType * get(Type *ElementType, unsigned NumElements)
static StructType * create(LLVMContext &Context, StringRef Name)
StructType::create - This creates an identified struct.
virtual void eraseFromParent()=0
bool hasDLLImportLinkage() const
unsigned getNumOperands() const
getNumOperands - Return the number of NamedMDNode operands.
bool Materialize(std::string *ErrInfo=0)
unsigned getNumElements() const
Random access to the elements.
GlobalValue * getNamedValue(StringRef Name) const
bool erase(const KeyT &Val)
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=0)
void setPrefixData(Constant *PrefixData)