mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-22 18:54:02 +01:00
Use references now that it is natural to do so.
The linker never takes ownership of a module or changes which module it is refering to, making it natural to use references. llvm-svn: 254449
This commit is contained in:
parent
91b4595716
commit
e3fda2ca99
@ -68,10 +68,10 @@ public:
|
||||
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
|
||||
@ -80,19 +80,19 @@ public:
|
||||
/// 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);
|
||||
|
||||
static bool LinkModules(Module *Dest, Module *Src,
|
||||
static bool linkModules(Module &Dest, Module &Src,
|
||||
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:
|
||||
Module *Composite;
|
||||
Module &Composite;
|
||||
|
||||
IdentifiedStructTypeSet IdentifiedStructTypes;
|
||||
|
||||
|
@ -67,14 +67,14 @@ const char* LTOCodeGenerator::getVersionString() {
|
||||
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)),
|
||||
IRLinker(new Linker(MergedModule.get())) {
|
||||
IRLinker(new Linker(*MergedModule)) {
|
||||
initializeLTOPasses();
|
||||
}
|
||||
|
||||
@ -114,7 +114,7 @@ bool LTOCodeGenerator::addModule(LTOModule *Mod) {
|
||||
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)
|
||||
@ -130,7 +130,7 @@ void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
|
||||
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)
|
||||
|
@ -390,7 +390,8 @@ void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
|
||||
/// This is an implementation class for the LinkModules function, which is the
|
||||
/// entrypoint for this file.
|
||||
class ModuleLinker {
|
||||
Module *DstM, *SrcM;
|
||||
Module &DstM;
|
||||
Module &SrcM;
|
||||
|
||||
TypeMapTy TypeMap;
|
||||
ValueMaterializerTy ValMaterializer;
|
||||
@ -431,11 +432,11 @@ class ModuleLinker {
|
||||
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)
|
||||
: 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) {
|
||||
@ -446,7 +447,7 @@ public:
|
||||
// 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);
|
||||
}
|
||||
|
||||
bool run();
|
||||
@ -485,7 +486,7 @@ private:
|
||||
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,
|
||||
@ -508,7 +509,7 @@ private:
|
||||
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;
|
||||
|
||||
@ -793,7 +794,7 @@ ModuleLinker::copyGlobalVariableProto(TypeMapTy &TypeMap,
|
||||
// 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());
|
||||
@ -808,7 +809,7 @@ Function *ModuleLinker::copyFunctionProto(TypeMapTy &TypeMap,
|
||||
// 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),
|
||||
getName(SF), DstM);
|
||||
getName(SF), &DstM);
|
||||
}
|
||||
|
||||
/// Set up prototypes for any aliases that come over from the source module.
|
||||
@ -842,7 +843,7 @@ GlobalValue *ModuleLinker::copyGlobalAliasProto(TypeMapTy &TypeMap,
|
||||
// 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
|
||||
@ -936,9 +937,9 @@ void ModuleLinker::materializeInitFor(GlobalValue *New, GlobalValue *Old) {
|
||||
linkGlobalValueBody(*Old);
|
||||
}
|
||||
|
||||
bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName,
|
||||
bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName,
|
||||
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)
|
||||
@ -997,8 +998,8 @@ bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
|
||||
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 =
|
||||
@ -1030,7 +1031,7 @@ bool ModuleLinker::getComdatResult(const Comdat *SrcC,
|
||||
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()) {
|
||||
@ -1158,7 +1159,7 @@ bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
|
||||
/// 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;
|
||||
@ -1174,12 +1175,12 @@ void ModuleLinker::computeTypeMapping() {
|
||||
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());
|
||||
}
|
||||
|
||||
for (GlobalValue &SGV : SrcM->aliases()) {
|
||||
for (GlobalValue &SGV : SrcM.aliases()) {
|
||||
if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
|
||||
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
|
||||
}
|
||||
@ -1188,7 +1189,7 @@ void ModuleLinker::computeTypeMapping() {
|
||||
// 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;
|
||||
@ -1201,7 +1202,7 @@ void ModuleLinker::computeTypeMapping() {
|
||||
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));
|
||||
if (!DST)
|
||||
continue;
|
||||
|
||||
@ -1275,10 +1276,10 @@ static void upgradeGlobalArray(GlobalVariable *GV) {
|
||||
|
||||
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));
|
||||
if (!DstGV)
|
||||
return;
|
||||
auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM->getNamedValue(Name));
|
||||
auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM.getNamedValue(Name));
|
||||
if (!SrcGV)
|
||||
return;
|
||||
|
||||
@ -1380,7 +1381,7 @@ bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
|
||||
|
||||
// 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());
|
||||
|
||||
@ -1430,7 +1431,7 @@ bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
|
||||
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;
|
||||
@ -1606,12 +1607,12 @@ bool ModuleLinker::linkGlobalValueBody(GlobalValue &Src) {
|
||||
|
||||
/// 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;
|
||||
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(
|
||||
@ -1623,12 +1624,12 @@ void ModuleLinker::linkNamedMDNodes() {
|
||||
/// 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.
|
||||
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));
|
||||
@ -1711,7 +1712,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
|
||||
|
||||
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;
|
||||
};
|
||||
@ -1744,7 +1745,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
|
||||
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: {
|
||||
@ -1754,7 +1755,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
|
||||
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;
|
||||
}
|
||||
@ -1833,51 +1834,47 @@ bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
|
||||
}
|
||||
|
||||
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.
|
||||
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: '" +
|
||||
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.
|
||||
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: " +
|
||||
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.
|
||||
if (!SrcM->getModuleInlineAsm().empty()) {
|
||||
if (DstM->getModuleInlineAsm().empty())
|
||||
DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
|
||||
if (!SrcM.getModuleInlineAsm().empty()) {
|
||||
if (DstM.getModuleInlineAsm().empty())
|
||||
DstM.setModuleInlineAsm(SrcM.getModuleInlineAsm());
|
||||
else
|
||||
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();
|
||||
for (const auto &SMEC : SrcM->getComdatSymbolTable()) {
|
||||
for (const auto &SMEC : SrcM.getComdatSymbolTable()) {
|
||||
const Comdat &C = SMEC.getValue();
|
||||
if (ComdatsChosen.count(&C))
|
||||
continue;
|
||||
@ -1891,37 +1888,37 @@ bool ModuleLinker::run() {
|
||||
// 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);
|
||||
|
||||
for (Function &SF : *SrcM)
|
||||
for (Function &SF : SrcM)
|
||||
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).
|
||||
for (GlobalVariable &GV : SrcM->globals())
|
||||
for (GlobalVariable &GV : SrcM.globals())
|
||||
if (linkIfNeeded(GV))
|
||||
return true;
|
||||
|
||||
for (Function &SF : *SrcM)
|
||||
for (Function &SF : SrcM)
|
||||
if (linkIfNeeded(SF))
|
||||
return true;
|
||||
|
||||
for (GlobalAlias &GA : SrcM->aliases())
|
||||
for (GlobalAlias &GA : SrcM.aliases())
|
||||
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 GlobalValue *GV = SrcM->getNamedValue(C.getName());
|
||||
const GlobalValue *GV = SrcM.getNamedValue(C.getName());
|
||||
if (GV)
|
||||
MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
|
||||
}
|
||||
@ -2032,12 +2029,10 @@ bool Linker::IdentifiedStructTypeSet::hasType(StructType *Ty) {
|
||||
return *I == Ty;
|
||||
}
|
||||
|
||||
Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
|
||||
this->Composite = M;
|
||||
this->DiagnosticHandler = DiagnosticHandler;
|
||||
|
||||
Linker::Linker(Module &M, DiagnosticHandlerFunction DiagnosticHandler)
|
||||
: Composite(M), DiagnosticHandler(DiagnosticHandler) {
|
||||
TypeFinder StructTypes;
|
||||
StructTypes.run(*M, true);
|
||||
StructTypes.run(M, true);
|
||||
for (StructType *Ty : StructTypes) {
|
||||
if (Ty->isOpaque())
|
||||
IdentifiedStructTypes.addOpaque(Ty);
|
||||
@ -2046,18 +2041,18 @@ Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
|
||||
}
|
||||
}
|
||||
|
||||
Linker::Linker(Module *M)
|
||||
Linker::Linker(Module &M)
|
||||
: 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();
|
||||
Composite->dropTriviallyDeadConstantArrays();
|
||||
Composite.dropTriviallyDeadConstantArrays();
|
||||
return RetCode;
|
||||
}
|
||||
|
||||
@ -2070,14 +2065,14 @@ bool Linker::linkInModule(Module *Src, unsigned Flags,
|
||||
/// 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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@ -2093,8 +2088,8 @@ LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
|
||||
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();
|
||||
|
@ -102,7 +102,7 @@ bool FunctionImporter::importFunctions(Module &M) {
|
||||
/// 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();
|
||||
@ -182,7 +182,7 @@ bool FunctionImporter::importFunctions(Module &M) {
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
@ -132,7 +132,7 @@ bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
|
||||
if (!M.get()) return true;
|
||||
|
||||
outs() << "Linking in input file: '" << Filenames[i] << "'\n";
|
||||
if (Linker::LinkModules(Program, M.get()))
|
||||
if (Linker::linkModules(*Program, *M))
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -222,7 +222,7 @@ static Module *TestMergedProgram(const BugDriver &BD, Module *M1, Module *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.
|
||||
|
||||
@ -390,7 +390,7 @@ static bool ExtractLoops(BugDriver &BD,
|
||||
MisCompFunctions.emplace_back(F->getName(), F->getFunctionType());
|
||||
}
|
||||
|
||||
if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted))
|
||||
if (Linker::linkModules(*ToNotOptimize, *ToOptimizeLoopExtracted))
|
||||
exit(1);
|
||||
|
||||
MiscompiledFunctions.clear();
|
||||
@ -418,7 +418,7 @@ static bool ExtractLoops(BugDriver &BD,
|
||||
// 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;
|
||||
@ -594,7 +594,7 @@ static bool ExtractBlocks(BugDriver &BD,
|
||||
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.
|
||||
|
@ -938,7 +938,7 @@ static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
|
||||
}
|
||||
|
||||
std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
|
||||
Linker L(Combined.get());
|
||||
Linker L(*Combined);
|
||||
|
||||
std::string DefaultTriple = sys::getDefaultTargetTriple();
|
||||
|
||||
@ -956,7 +956,7 @@ static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
|
||||
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");
|
||||
@ -986,7 +986,7 @@ static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
|
||||
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;
|
||||
}
|
||||
|
@ -198,7 +198,7 @@ static bool importFunctions(const char *argv0, LLVMContext &Context,
|
||||
}
|
||||
|
||||
// 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;
|
||||
@ -238,7 +238,7 @@ static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
|
||||
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;
|
||||
@ -266,7 +266,7 @@ int main(int argc, char **argv) {
|
||||
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)
|
||||
|
@ -93,7 +93,7 @@ TEST_F(LinkModuleTest, BlockAddress) {
|
||||
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();
|
||||
@ -169,13 +169,13 @@ static Module *getInternal(LLVMContext &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));
|
||||
Linker::LinkModules(InternalM.get(), EmptyM.get());
|
||||
Linker::linkModules(*InternalM, *EmptyM);
|
||||
}
|
||||
|
||||
TEST_F(LinkModuleTest, TypeMerge) {
|
||||
@ -190,7 +190,7 @@ TEST_F(LinkModuleTest, TypeMerge) {
|
||||
"@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());
|
||||
@ -267,8 +267,7 @@ TEST_F(LinkModuleTest, MoveDistinctMDs) {
|
||||
// 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.
|
||||
|
Loading…
Reference in New Issue
Block a user