The linker never takes ownership of a module or changes which module it
is refering to, making it natural to use references.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254449
91177308-0d34-0410-b5e6-
96231b3b80d8
InternalizeLinkedSymbols = (1 << 2)
};
InternalizeLinkedSymbols = (1 << 2)
};
- Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler);
- Linker(Module *M);
+ Linker(Module &M, DiagnosticHandlerFunction DiagnosticHandler);
+ Linker(Module &M);
- Module *getModule() const { return Composite; }
+ Module &getModule() const { return Composite; }
/// \brief Link \p Src into the composite. The source is destroyed.
/// Passing OverrideSymbols as true will have symbols from Src
/// \brief Link \p Src into the composite. The source is destroyed.
/// Passing OverrideSymbols as true will have symbols from Src
/// is passed. If a \p FuncToImport is provided, only that single
/// function is imported from the source module.
/// Returns true on error.
/// is passed. If a \p FuncToImport is provided, only that single
/// function is imported from the source module.
/// Returns true on error.
- bool linkInModule(Module *Src, unsigned Flags = Flags::None,
+ bool linkInModule(Module &Src, unsigned Flags = Flags::None,
const FunctionInfoIndex *Index = nullptr,
Function *FuncToImport = nullptr);
const FunctionInfoIndex *Index = nullptr,
Function *FuncToImport = nullptr);
- static bool LinkModules(Module *Dest, Module *Src,
+ static bool linkModules(Module &Dest, Module &Src,
DiagnosticHandlerFunction DiagnosticHandler,
unsigned Flags = Flags::None);
DiagnosticHandlerFunction DiagnosticHandler,
unsigned Flags = Flags::None);
- static bool LinkModules(Module *Dest, Module *Src,
+ static bool linkModules(Module &Dest, Module &Src,
unsigned Flags = Flags::None);
private:
unsigned Flags = Flags::None);
private:
IdentifiedStructTypeSet IdentifiedStructTypes;
IdentifiedStructTypeSet IdentifiedStructTypes;
LTOCodeGenerator::LTOCodeGenerator()
: Context(getGlobalContext()),
MergedModule(new Module("ld-temp.o", Context)),
LTOCodeGenerator::LTOCodeGenerator()
: Context(getGlobalContext()),
MergedModule(new Module("ld-temp.o", Context)),
- IRLinker(new Linker(MergedModule.get())) {
+ IRLinker(new Linker(*MergedModule)) {
initializeLTOPasses();
}
LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
: OwnedContext(std::move(Context)), Context(*OwnedContext),
MergedModule(new Module("ld-temp.o", *OwnedContext)),
initializeLTOPasses();
}
LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
: OwnedContext(std::move(Context)), Context(*OwnedContext),
MergedModule(new Module("ld-temp.o", *OwnedContext)),
- IRLinker(new Linker(MergedModule.get())) {
+ IRLinker(new Linker(*MergedModule)) {
assert(&Mod->getModule().getContext() == &Context &&
"Expected module in same context");
assert(&Mod->getModule().getContext() == &Context &&
"Expected module in same context");
- bool ret = IRLinker->linkInModule(&Mod->getModule());
+ bool ret = IRLinker->linkInModule(Mod->getModule());
const std::vector<const char *> &undefs = Mod->getAsmUndefinedRefs();
for (int i = 0, e = undefs.size(); i != e; ++i)
const std::vector<const char *> &undefs = Mod->getAsmUndefinedRefs();
for (int i = 0, e = undefs.size(); i != e; ++i)
AsmUndefinedRefs.clear();
MergedModule = Mod->takeModule();
AsmUndefinedRefs.clear();
MergedModule = Mod->takeModule();
- IRLinker = make_unique<Linker>(MergedModule.get());
+ IRLinker = make_unique<Linker>(*MergedModule);
const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
for (int I = 0, E = Undefs.size(); I != E; ++I)
const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
for (int I = 0, E = Undefs.size(); I != E; ++I)
/// This is an implementation class for the LinkModules function, which is the
/// entrypoint for this file.
class ModuleLinker {
/// This is an implementation class for the LinkModules function, which is the
/// entrypoint for this file.
class ModuleLinker {
+ Module &DstM;
+ Module &SrcM;
TypeMapTy TypeMap;
ValueMaterializerTy ValMaterializer;
TypeMapTy TypeMap;
ValueMaterializerTy ValMaterializer;
bool HasError = false;
public:
bool HasError = false;
public:
- ModuleLinker(Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
+ ModuleLinker(Module &DstM, Linker::IdentifiedStructTypeSet &Set, Module &SrcM,
DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags,
const FunctionInfoIndex *Index = nullptr,
Function *FuncToImport = nullptr)
DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags,
const FunctionInfoIndex *Index = nullptr,
Function *FuncToImport = nullptr)
- : DstM(dstM), SrcM(srcM), TypeMap(Set), ValMaterializer(this),
+ : DstM(DstM), SrcM(SrcM), TypeMap(Set), ValMaterializer(this),
DiagnosticHandler(DiagnosticHandler), Flags(Flags), ImportIndex(Index),
ImportFunction(FuncToImport), HasExportedFunctions(false),
DoneLinkingBodies(false) {
DiagnosticHandler(DiagnosticHandler), Flags(Flags), ImportIndex(Index),
ImportFunction(FuncToImport), HasExportedFunctions(false),
DoneLinkingBodies(false) {
// backend compilation, and we need to see if it has functions that
// may be exported to another backend compilation.
if (ImportIndex && !ImportFunction)
// backend compilation, and we need to see if it has functions that
// may be exported to another backend compilation.
if (ImportIndex && !ImportFunction)
- HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM);
+ HasExportedFunctions = ImportIndex->hasExportedFunctions(&SrcM);
DiagnosticHandler(LinkDiagnosticInfo(DS_Warning, Message));
}
DiagnosticHandler(LinkDiagnosticInfo(DS_Warning, Message));
}
- bool getComdatLeader(Module *M, StringRef ComdatName,
+ bool getComdatLeader(Module &M, StringRef ComdatName,
const GlobalVariable *&GVar);
bool computeResultingSelectionKind(StringRef ComdatName,
Comdat::SelectionKind Src,
const GlobalVariable *&GVar);
bool computeResultingSelectionKind(StringRef ComdatName,
Comdat::SelectionKind Src,
return nullptr;
// Otherwise see if we have a match in the destination module's symtab.
return nullptr;
// Otherwise see if we have a match in the destination module's symtab.
- GlobalValue *DGV = DstM->getNamedValue(getName(SrcGV));
+ GlobalValue *DGV = DstM.getNamedValue(getName(SrcGV));
if (!DGV)
return nullptr;
if (!DGV)
return nullptr;
// identical version of the symbol over in the dest module... the
// initializer will be filled in later by LinkGlobalInits.
GlobalVariable *NewDGV = new GlobalVariable(
// identical version of the symbol over in the dest module... the
// initializer will be filled in later by LinkGlobalInits.
GlobalVariable *NewDGV = new GlobalVariable(
- *DstM, TypeMap.get(SGVar->getType()->getElementType()),
+ DstM, TypeMap.get(SGVar->getType()->getElementType()),
SGVar->isConstant(), getLinkage(SGVar), /*init*/ nullptr, getName(SGVar),
/*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
SGVar->getType()->getAddressSpace());
SGVar->isConstant(), getLinkage(SGVar), /*init*/ nullptr, getName(SGVar),
/*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
SGVar->getType()->getAddressSpace());
// If there is no linkage to be performed or we are linking from the source,
// bring SF over.
return Function::Create(TypeMap.get(SF->getFunctionType()), getLinkage(SF),
// If there is no linkage to be performed or we are linking from the source,
// bring SF over.
return Function::Create(TypeMap.get(SF->getFunctionType()), getLinkage(SF),
}
/// Set up prototypes for any aliases that come over from the source module.
}
/// Set up prototypes for any aliases that come over from the source module.
// bring over SGA.
auto *Ty = TypeMap.get(SGA->getValueType());
return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
// bring over SGA.
auto *Ty = TypeMap.get(SGA->getValueType());
return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
- getLinkage(SGA), getName(SGA), DstM);
+ getLinkage(SGA), getName(SGA), &DstM);
}
static GlobalValue::VisibilityTypes
}
static GlobalValue::VisibilityTypes
linkGlobalValueBody(*Old);
}
linkGlobalValueBody(*Old);
}
-bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName,
+bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName,
const GlobalVariable *&GVar) {
const GlobalVariable *&GVar) {
- const GlobalValue *GVal = M->getNamedValue(ComdatName);
+ const GlobalValue *GVal = M.getNamedValue(ComdatName);
if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
GVal = GA->getBaseObject();
if (!GVal)
if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
GVal = GA->getBaseObject();
if (!GVal)
getComdatLeader(SrcM, ComdatName, SrcGV))
return true;
getComdatLeader(SrcM, ComdatName, SrcGV))
return true;
- const DataLayout &DstDL = DstM->getDataLayout();
- const DataLayout &SrcDL = SrcM->getDataLayout();
+ const DataLayout &DstDL = DstM.getDataLayout();
+ const DataLayout &SrcDL = SrcM.getDataLayout();
uint64_t DstSize =
DstDL.getTypeAllocSize(DstGV->getType()->getPointerElementType());
uint64_t SrcSize =
uint64_t DstSize =
DstDL.getTypeAllocSize(DstGV->getType()->getPointerElementType());
uint64_t SrcSize =
bool &LinkFromSrc) {
Comdat::SelectionKind SSK = SrcC->getSelectionKind();
StringRef ComdatName = SrcC->getName();
bool &LinkFromSrc) {
Comdat::SelectionKind SSK = SrcC->getSelectionKind();
StringRef ComdatName = SrcC->getName();
- Module::ComdatSymTabType &ComdatSymTab = DstM->getComdatSymbolTable();
+ Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable();
Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
if (DstCI == ComdatSymTab.end()) {
Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
if (DstCI == ComdatSymTab.end()) {
/// types 'Foo' but one got renamed when the module was loaded into the same
/// LLVMContext.
void ModuleLinker::computeTypeMapping() {
/// types 'Foo' but one got renamed when the module was loaded into the same
/// LLVMContext.
void ModuleLinker::computeTypeMapping() {
- for (GlobalValue &SGV : SrcM->globals()) {
+ for (GlobalValue &SGV : SrcM.globals()) {
GlobalValue *DGV = getLinkedToGlobal(&SGV);
if (!DGV)
continue;
GlobalValue *DGV = getLinkedToGlobal(&SGV);
if (!DGV)
continue;
TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
}
TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
}
- for (GlobalValue &SGV : *SrcM) {
+ for (GlobalValue &SGV : SrcM) {
if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
}
if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
}
- for (GlobalValue &SGV : SrcM->aliases()) {
+ for (GlobalValue &SGV : SrcM.aliases()) {
if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
}
if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
}
// At this point, the destination module may have a type "%foo = { i32 }" for
// example. When the source module got loaded into the same LLVMContext, if
// it had the same type, it would have been renamed to "%foo.42 = { i32 }".
// At this point, the destination module may have a type "%foo = { i32 }" for
// example. When the source module got loaded into the same LLVMContext, if
// it had the same type, it would have been renamed to "%foo.42 = { i32 }".
- std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
+ std::vector<StructType *> Types = SrcM.getIdentifiedStructTypes();
for (StructType *ST : Types) {
if (!ST->hasName())
continue;
for (StructType *ST : Types) {
if (!ST->hasName())
continue;
continue;
// Check to see if the destination module has a struct with the prefix name.
continue;
// Check to see if the destination module has a struct with the prefix name.
- StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos));
+ StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos));
void ModuleLinker::upgradeMismatchedGlobalArray(StringRef Name) {
// Look for the global arrays.
void ModuleLinker::upgradeMismatchedGlobalArray(StringRef Name) {
// Look for the global arrays.
- auto *DstGV = dyn_cast_or_null<GlobalVariable>(DstM->getNamedValue(Name));
+ auto *DstGV = dyn_cast_or_null<GlobalVariable>(DstM.getNamedValue(Name));
- auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM->getNamedValue(Name));
+ auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM.getNamedValue(Name));
// Create the new global variable.
GlobalVariable *NG = new GlobalVariable(
// Create the new global variable.
GlobalVariable *NG = new GlobalVariable(
- *DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
+ DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
/*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
SrcGV->getType()->getAddressSpace());
/*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
SrcGV->getType()->getAddressSpace());
if (const Comdat *SC = SGV->getComdat()) {
Comdat::SelectionKind SK;
std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
if (const Comdat *SC = SGV->getComdat()) {
Comdat::SelectionKind SK;
std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
- C = DstM->getOrInsertComdat(SC->getName());
+ C = DstM.getOrInsertComdat(SC->getName());
C->setSelectionKind(SK);
if (SGV->hasInternalLinkage())
LinkFromSrc = true;
C->setSelectionKind(SK);
if (SGV->hasInternalLinkage())
LinkFromSrc = true;
/// Insert all of the named MDNodes in Src into the Dest module.
void ModuleLinker::linkNamedMDNodes() {
/// Insert all of the named MDNodes in Src into the Dest module.
void ModuleLinker::linkNamedMDNodes() {
- const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
- for (const NamedMDNode &NMD : SrcM->named_metadata()) {
+ const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
+ for (const NamedMDNode &NMD : SrcM.named_metadata()) {
// Don't link module flags here. Do them separately.
if (&NMD == SrcModFlags)
continue;
// Don't link module flags here. Do them separately.
if (&NMD == SrcModFlags)
continue;
- NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(NMD.getName());
+ NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
// Add Src elements into Dest node.
for (const MDNode *op : NMD.operands())
DestNMD->addOperand(MapMetadata(
// Add Src elements into Dest node.
for (const MDNode *op : NMD.operands())
DestNMD->addOperand(MapMetadata(
/// Merge the linker flags in Src into the Dest module.
bool ModuleLinker::linkModuleFlagsMetadata() {
// If the source module has no module flags, we are done.
/// Merge the linker flags in Src into the Dest module.
bool ModuleLinker::linkModuleFlagsMetadata() {
// If the source module has no module flags, we are done.
- const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
+ const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
if (!SrcModFlags) return false;
// If the destination module doesn't have module flags yet, then just copy
// over the source module's flags.
if (!SrcModFlags) return false;
// If the destination module doesn't have module flags yet, then just copy
// over the source module's flags.
- NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
+ NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
if (DstModFlags->getNumOperands() == 0) {
for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
DstModFlags->addOperand(SrcModFlags->getOperand(I));
if (DstModFlags->getNumOperands() == 0) {
for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
DstModFlags->addOperand(SrcModFlags->getOperand(I));
auto replaceDstValue = [&](MDNode *New) {
Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
auto replaceDstValue = [&](MDNode *New) {
Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
- MDNode *Flag = MDNode::get(DstM->getContext(), FlagOps);
+ MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
DstModFlags->setOperand(DstIndex, Flag);
Flags[ID].first = Flag;
};
DstModFlags->setOperand(DstIndex, Flag);
Flags[ID].first = Flag;
};
MDs.append(DstValue->op_begin(), DstValue->op_end());
MDs.append(SrcValue->op_begin(), SrcValue->op_end());
MDs.append(DstValue->op_begin(), DstValue->op_end());
MDs.append(SrcValue->op_begin(), SrcValue->op_end());
- replaceDstValue(MDNode::get(DstM->getContext(), MDs));
+ replaceDstValue(MDNode::get(DstM.getContext(), MDs));
break;
}
case Module::AppendUnique: {
break;
}
case Module::AppendUnique: {
Elts.insert(DstValue->op_begin(), DstValue->op_end());
Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
Elts.insert(DstValue->op_begin(), DstValue->op_end());
Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
- replaceDstValue(MDNode::get(DstM->getContext(),
+ replaceDstValue(MDNode::get(DstM.getContext(),
makeArrayRef(Elts.begin(), Elts.end())));
break;
}
makeArrayRef(Elts.begin(), Elts.end())));
break;
}
}
bool ModuleLinker::run() {
}
bool ModuleLinker::run() {
- assert(DstM && "Null destination module");
- assert(SrcM && "Null source module");
-
// Inherit the target data from the source module if the destination module
// doesn't have one already.
// Inherit the target data from the source module if the destination module
// doesn't have one already.
- if (DstM->getDataLayout().isDefault())
- DstM->setDataLayout(SrcM->getDataLayout());
+ if (DstM.getDataLayout().isDefault())
+ DstM.setDataLayout(SrcM.getDataLayout());
- if (SrcM->getDataLayout() != DstM->getDataLayout()) {
+ if (SrcM.getDataLayout() != DstM.getDataLayout()) {
emitWarning("Linking two modules of different data layouts: '" +
emitWarning("Linking two modules of different data layouts: '" +
- SrcM->getModuleIdentifier() + "' is '" +
- SrcM->getDataLayoutStr() + "' whereas '" +
- DstM->getModuleIdentifier() + "' is '" +
- DstM->getDataLayoutStr() + "'\n");
+ SrcM.getModuleIdentifier() + "' is '" +
+ SrcM.getDataLayoutStr() + "' whereas '" +
+ DstM.getModuleIdentifier() + "' is '" +
+ DstM.getDataLayoutStr() + "'\n");
}
// Copy the target triple from the source to dest if the dest's is empty.
}
// Copy the target triple from the source to dest if the dest's is empty.
- if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
- DstM->setTargetTriple(SrcM->getTargetTriple());
+ if (DstM.getTargetTriple().empty() && !SrcM.getTargetTriple().empty())
+ DstM.setTargetTriple(SrcM.getTargetTriple());
- Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM->getTargetTriple());
+ Triple SrcTriple(SrcM.getTargetTriple()), DstTriple(DstM.getTargetTriple());
- if (!SrcM->getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
+ if (!SrcM.getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
emitWarning("Linking two modules of different target triples: " +
emitWarning("Linking two modules of different target triples: " +
- SrcM->getModuleIdentifier() + "' is '" +
- SrcM->getTargetTriple() + "' whereas '" +
- DstM->getModuleIdentifier() + "' is '" +
- DstM->getTargetTriple() + "'\n");
+ SrcM.getModuleIdentifier() + "' is '" + SrcM.getTargetTriple() +
+ "' whereas '" + DstM.getModuleIdentifier() + "' is '" +
+ DstM.getTargetTriple() + "'\n");
- DstM->setTargetTriple(mergeTriples(SrcTriple, DstTriple));
+ DstM.setTargetTriple(mergeTriples(SrcTriple, DstTriple));
// Append the module inline asm string.
// Append the module inline asm string.
- if (!SrcM->getModuleInlineAsm().empty()) {
- if (DstM->getModuleInlineAsm().empty())
- DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
+ if (!SrcM.getModuleInlineAsm().empty()) {
+ if (DstM.getModuleInlineAsm().empty())
+ DstM.setModuleInlineAsm(SrcM.getModuleInlineAsm());
- DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
- SrcM->getModuleInlineAsm());
+ DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
+ SrcM.getModuleInlineAsm());
}
// Loop over all of the linked values to compute type mappings.
computeTypeMapping();
ComdatsChosen.clear();
}
// Loop over all of the linked values to compute type mappings.
computeTypeMapping();
ComdatsChosen.clear();
- for (const auto &SMEC : SrcM->getComdatSymbolTable()) {
+ for (const auto &SMEC : SrcM.getComdatSymbolTable()) {
const Comdat &C = SMEC.getValue();
if (ComdatsChosen.count(&C))
continue;
const Comdat &C = SMEC.getValue();
if (ComdatsChosen.count(&C))
continue;
// Upgrade mismatched global arrays.
upgradeMismatchedGlobals();
// Upgrade mismatched global arrays.
upgradeMismatchedGlobals();
- for (GlobalVariable &GV : SrcM->globals())
+ for (GlobalVariable &GV : SrcM.globals())
if (const Comdat *SC = GV.getComdat())
ComdatMembers[SC].push_back(&GV);
if (const Comdat *SC = GV.getComdat())
ComdatMembers[SC].push_back(&GV);
- for (Function &SF : *SrcM)
+ for (Function &SF : SrcM)
if (const Comdat *SC = SF.getComdat())
ComdatMembers[SC].push_back(&SF);
if (const Comdat *SC = SF.getComdat())
ComdatMembers[SC].push_back(&SF);
- for (GlobalAlias &GA : SrcM->aliases())
+ for (GlobalAlias &GA : SrcM.aliases())
if (const Comdat *SC = GA.getComdat())
ComdatMembers[SC].push_back(&GA);
// Insert all of the globals in src into the DstM module... without linking
// initializers (which could refer to functions not yet mapped over).
if (const Comdat *SC = GA.getComdat())
ComdatMembers[SC].push_back(&GA);
// Insert all of the globals in src into the DstM module... without linking
// initializers (which could refer to functions not yet mapped over).
- for (GlobalVariable &GV : SrcM->globals())
+ for (GlobalVariable &GV : SrcM.globals())
if (linkIfNeeded(GV))
return true;
if (linkIfNeeded(GV))
return true;
- for (Function &SF : *SrcM)
+ for (Function &SF : SrcM)
if (linkIfNeeded(SF))
return true;
if (linkIfNeeded(SF))
return true;
- for (GlobalAlias &GA : SrcM->aliases())
+ for (GlobalAlias &GA : SrcM.aliases())
if (linkIfNeeded(GA))
return true;
if (linkIfNeeded(GA))
return true;
- for (const auto &Entry : DstM->getComdatSymbolTable()) {
+ for (const auto &Entry : DstM.getComdatSymbolTable()) {
const Comdat &C = Entry.getValue();
if (C.getSelectionKind() == Comdat::Any)
continue;
const Comdat &C = Entry.getValue();
if (C.getSelectionKind() == Comdat::Any)
continue;
- const GlobalValue *GV = SrcM->getNamedValue(C.getName());
+ const GlobalValue *GV = SrcM.getNamedValue(C.getName());
if (GV)
MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
}
if (GV)
MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
}
-Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
- this->Composite = M;
- this->DiagnosticHandler = DiagnosticHandler;
-
+Linker::Linker(Module &M, DiagnosticHandlerFunction DiagnosticHandler)
+ : Composite(M), DiagnosticHandler(DiagnosticHandler) {
- StructTypes.run(*M, true);
+ StructTypes.run(M, true);
for (StructType *Ty : StructTypes) {
if (Ty->isOpaque())
IdentifiedStructTypes.addOpaque(Ty);
for (StructType *Ty : StructTypes) {
if (Ty->isOpaque())
IdentifiedStructTypes.addOpaque(Ty);
-Linker::Linker(Module *M)
+Linker::Linker(Module &M)
: Linker(M, [this](const DiagnosticInfo &DI) {
: Linker(M, [this](const DiagnosticInfo &DI) {
- Composite->getContext().diagnose(DI);
+ Composite.getContext().diagnose(DI);
-bool Linker::linkInModule(Module *Src, unsigned Flags,
+bool Linker::linkInModule(Module &Src, unsigned Flags,
const FunctionInfoIndex *Index,
Function *FuncToImport) {
ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src,
DiagnosticHandler, Flags, Index, FuncToImport);
bool RetCode = TheLinker.run();
const FunctionInfoIndex *Index,
Function *FuncToImport) {
ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src,
DiagnosticHandler, Flags, Index, FuncToImport);
bool RetCode = TheLinker.run();
- Composite->dropTriviallyDeadConstantArrays();
+ Composite.dropTriviallyDeadConstantArrays();
/// true is returned and ErrorMsg (if not null) is set to indicate the problem.
/// Upon failure, the Dest module could be in a modified state, and shouldn't be
/// relied on to be consistent.
/// true is returned and ErrorMsg (if not null) is set to indicate the problem.
/// Upon failure, the Dest module could be in a modified state, and shouldn't be
/// relied on to be consistent.
-bool Linker::LinkModules(Module *Dest, Module *Src,
+bool Linker::linkModules(Module &Dest, Module &Src,
DiagnosticHandlerFunction DiagnosticHandler,
unsigned Flags) {
Linker L(Dest, DiagnosticHandler);
return L.linkInModule(Src, Flags);
}
DiagnosticHandlerFunction DiagnosticHandler,
unsigned Flags) {
Linker L(Dest, DiagnosticHandler);
return L.linkInModule(Src, Flags);
}
-bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Flags) {
+bool Linker::linkModules(Module &Dest, Module &Src, unsigned Flags) {
Linker L(Dest);
return L.linkInModule(Src, Flags);
}
Linker L(Dest);
return L.linkInModule(Src, Flags);
}
raw_string_ostream Stream(Message);
DiagnosticPrinterRawOStream DP(Stream);
raw_string_ostream Stream(Message);
DiagnosticPrinterRawOStream DP(Stream);
- LLVMBool Result = Linker::LinkModules(
- D, unwrap(Src), [&](const DiagnosticInfo &DI) { DI.print(DP); });
+ LLVMBool Result = Linker::linkModules(
+ *D, *unwrap(Src), [&](const DiagnosticInfo &DI) { DI.print(DP); });
if (OutMessages && Result) {
Stream.flush();
if (OutMessages && Result) {
Stream.flush();
/// Second step: for every call to an external function, try to import it.
// Linker that will be used for importing function
/// Second step: for every call to an external function, try to import it.
// Linker that will be used for importing function
- Linker L(&M, DiagnosticHandler);
+ Linker L(M, DiagnosticHandler);
while (!Worklist.empty()) {
auto CalledFunctionName = Worklist.pop_back_val();
while (!Worklist.empty()) {
auto CalledFunctionName = Worklist.pop_back_val();
}
// Link in the specified function.
}
// Link in the specified function.
- if (L.linkInModule(&Module, Linker::Flags::None, &Index, F))
+ if (L.linkInModule(Module, Linker::Flags::None, &Index, F))
report_fatal_error("Function Import: link error");
// Process the newly imported function and add callees to the worklist.
report_fatal_error("Function Import: link error");
// Process the newly imported function and add callees to the worklist.
if (!M.get()) return true;
outs() << "Linking in input file: '" << Filenames[i] << "'\n";
if (!M.get()) return true;
outs() << "Linking in input file: '" << Filenames[i] << "'\n";
- if (Linker::LinkModules(Program, M.get()))
+ if (Linker::linkModules(*Program, *M))
M1 = CloneModule(M1);
M2 = CloneModule(M2);
}
M1 = CloneModule(M1);
M2 = CloneModule(M2);
}
- if (Linker::LinkModules(M1, M2))
+ if (Linker::linkModules(*M1, *M2))
exit(1);
delete M2; // We are done with this module.
exit(1);
delete M2; // We are done with this module.
MisCompFunctions.emplace_back(F->getName(), F->getFunctionType());
}
MisCompFunctions.emplace_back(F->getName(), F->getFunctionType());
}
- if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted))
+ if (Linker::linkModules(*ToNotOptimize, *ToOptimizeLoopExtracted))
exit(1);
MiscompiledFunctions.clear();
exit(1);
MiscompiledFunctions.clear();
// extraction both didn't break the program, and didn't mask the problem.
// Replace the current program with the loop extracted version, and try to
// extract another loop.
// extraction both didn't break the program, and didn't mask the problem.
// Replace the current program with the loop extracted version, and try to
// extract another loop.
- if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted))
+ if (Linker::linkModules(*ToNotOptimize, *ToOptimizeLoopExtracted))
exit(1);
delete ToOptimizeLoopExtracted;
exit(1);
delete ToOptimizeLoopExtracted;
if (!I->isDeclaration())
MisCompFunctions.emplace_back(I->getName(), I->getFunctionType());
if (!I->isDeclaration())
MisCompFunctions.emplace_back(I->getName(), I->getFunctionType());
- if (Linker::LinkModules(ProgClone, Extracted.get()))
+ if (Linker::linkModules(*ProgClone, *Extracted))
exit(1);
// Set the new program and delete the old one.
exit(1);
// Set the new program and delete the old one.
}
std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
}
std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
- Linker L(Combined.get());
std::string DefaultTriple = sys::getDefaultTargetTriple();
std::string DefaultTriple = sys::getDefaultTargetTriple();
M->setTargetTriple(DefaultTriple);
}
M->setTargetTriple(DefaultTriple);
}
- if (L.linkInModule(M.get()))
+ if (L.linkInModule(*M))
message(LDPL_FATAL, "Failed to link module");
if (release_input_file(F.handle) != LDPS_OK)
message(LDPL_FATAL, "Failed to release file information");
message(LDPL_FATAL, "Failed to link module");
if (release_input_file(F.handle) != LDPS_OK)
message(LDPL_FATAL, "Failed to release file information");
path = output_name;
else
path = output_name + ".bc";
path = output_name;
else
path = output_name + ".bc";
- saveBCFile(path, *L.getModule());
+ saveBCFile(path, *Combined);
if (options::TheOutputType == options::OT_BC_ONLY)
return LDPS_OK;
}
if (options::TheOutputType == options::OT_BC_ONLY)
return LDPS_OK;
}
}
// Link in the specified function.
}
// Link in the specified function.
- if (L.linkInModule(M.get(), Linker::Flags::None, Index.get(), F))
+ if (L.linkInModule(*M, Linker::Flags::None, Index.get(), F))
return false;
}
return true;
return false;
}
return true;
if (Verbose)
errs() << "Linking in '" << File << "'\n";
if (Verbose)
errs() << "Linking in '" << File << "'\n";
- if (L.linkInModule(M.get(), ApplicableFlags, Index.get()))
+ if (L.linkInModule(*M, ApplicableFlags, Index.get()))
return false;
// All linker flags apply to linking of subsequent files.
ApplicableFlags = Flags;
return false;
// All linker flags apply to linking of subsequent files.
ApplicableFlags = Flags;
cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
auto Composite = make_unique<Module>("llvm-link", Context);
cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
auto Composite = make_unique<Module>("llvm-link", Context);
- Linker L(Composite.get(), diagnosticHandler);
+ Linker L(*Composite, diagnosticHandler);
unsigned Flags = Linker::Flags::None;
if (Internalize)
unsigned Flags = Linker::Flags::None;
if (Internalize)
Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
Module *LinkedModule = new Module("MyModuleLinked", Ctx);
Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
Module *LinkedModule = new Module("MyModuleLinked", Ctx);
- Linker::LinkModules(LinkedModule, M.get());
+ Linker::linkModules(*LinkedModule, *M);
// Delete the original module.
M.reset();
// Delete the original module.
M.reset();
TEST_F(LinkModuleTest, EmptyModule) {
std::unique_ptr<Module> InternalM(getInternal(Ctx));
std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
TEST_F(LinkModuleTest, EmptyModule) {
std::unique_ptr<Module> InternalM(getInternal(Ctx));
std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
- Linker::LinkModules(EmptyM.get(), InternalM.get());
+ Linker::linkModules(*EmptyM, *InternalM);
}
TEST_F(LinkModuleTest, EmptyModule2) {
std::unique_ptr<Module> InternalM(getInternal(Ctx));
std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
}
TEST_F(LinkModuleTest, EmptyModule2) {
std::unique_ptr<Module> InternalM(getInternal(Ctx));
std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
- Linker::LinkModules(InternalM.get(), EmptyM.get());
+ Linker::linkModules(*InternalM, *EmptyM);
}
TEST_F(LinkModuleTest, TypeMerge) {
}
TEST_F(LinkModuleTest, TypeMerge) {
"@t2 = weak global %t zeroinitializer\n";
std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C);
"@t2 = weak global %t zeroinitializer\n";
std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C);
- Linker::LinkModules(M1.get(), M2.get(), [](const llvm::DiagnosticInfo &){});
+ Linker::linkModules(*M1, *M2, [](const llvm::DiagnosticInfo &) {});
EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
M1->getNamedGlobal("t2")->getType());
EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
M1->getNamedGlobal("t2")->getType());
// Link into destination module.
auto Dst = llvm::make_unique<Module>("Linked", C);
ASSERT_TRUE(Dst.get());
// Link into destination module.
auto Dst = llvm::make_unique<Module>("Linked", C);
ASSERT_TRUE(Dst.get());
- Linker::LinkModules(Dst.get(), Src.get(),
- [](const llvm::DiagnosticInfo &) {});
+ Linker::linkModules(*Dst, *Src, [](const llvm::DiagnosticInfo &) {});
// Check that distinct metadata was moved, not cloned. Even !4, the uniqued
// node, should effectively be moved, since its only operand hasn't changed.
// Check that distinct metadata was moved, not cloned. Even !4, the uniqued
// node, should effectively be moved, since its only operand hasn't changed.