#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Constants.h"
+#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/LLVMContext.h"
// Vector of GlobalValues to lazily link in.
std::vector<GlobalValue *> LazilyLinkGlobalValues;
+ /// Functions that have replaced other functions.
+ SmallPtrSet<const Function *, 16> OverridingFunctions;
+
Linker::DiagnosticHandlerFunction DiagnosticHandler;
public:
bool linkGlobalValueBody(GlobalValue &Src);
void linkNamedMDNodes();
+ void stripReplacedSubprograms();
};
}
}
NewGV = copyGlobalValueProto(TypeMap, *DstM, SGV);
+
+ if (DGV && isa<Function>(DGV))
+ if (auto *NewF = dyn_cast<Function>(NewGV))
+ OverridingFunctions.insert(NewF);
}
NewGV->setUnnamedAddr(HasUnnamedAddr);
NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName());
// Add Src elements into Dest node.
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
- DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap,
- RF_None, &TypeMap, &ValMaterializer));
+ DestNMD->addOperand(MapMetadata(I->getOperand(i), ValueMap, RF_None,
+ &TypeMap, &ValMaterializer));
+ }
+}
+
+/// Drop DISubprograms that have been superseded.
+///
+/// FIXME: this creates an asymmetric result: we strip losing subprograms from
+/// DstM, but leave losing subprograms in SrcM. Instead we should also strip
+/// losers from SrcM, but this requires extra plumbing in MapMetadata.
+void ModuleLinker::stripReplacedSubprograms() {
+ // Avoid quadratic runtime by returning early when there's nothing to do.
+ if (OverridingFunctions.empty())
+ return;
+
+ // Move the functions now, so the set gets cleared even on early returns.
+ auto Functions = std::move(OverridingFunctions);
+ OverridingFunctions.clear();
+
+ // Drop subprograms whose functions have been overridden by the new compile
+ // unit.
+ NamedMDNode *CompileUnits = DstM->getNamedMetadata("llvm.dbg.cu");
+ if (!CompileUnits)
+ return;
+ for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
+ DICompileUnit CU(CompileUnits->getOperand(I));
+ assert(CU && "Expected valid compile unit");
+
+ DITypedArray<DISubprogram> SPs(CU.getSubprograms());
+ assert(SPs && "Expected valid subprogram array");
+
+ SmallVector<Metadata *, 16> NewSPs;
+ NewSPs.reserve(SPs.getNumElements());
+ for (unsigned S = 0, SE = SPs.getNumElements(); S != SE; ++S) {
+ DISubprogram SP = SPs.getElement(S);
+ if (SP && SP.getFunction() && Functions.count(SP.getFunction()))
+ continue;
+
+ NewSPs.push_back(SP);
+ }
+
+ // Redirect operand to the overriding subprogram.
+ if (NewSPs.size() != SPs.getNumElements())
+ CU.replaceSubprograms(DIArray(MDNode::get(DstM->getContext(), NewSPs)));
}
}
SmallSetVector<MDNode*, 16> Requirements;
for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
MDNode *Op = DstModFlags->getOperand(I);
- ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0));
+ ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
MDString *ID = cast<MDString>(Op->getOperand(1));
if (Behavior->getZExtValue() == Module::Require) {
bool HasErr = false;
for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
MDNode *SrcOp = SrcModFlags->getOperand(I);
- ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0));
+ ConstantInt *SrcBehavior =
+ mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
MDString *ID = cast<MDString>(SrcOp->getOperand(1));
MDNode *DstOp = Flags.lookup(ID);
unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
}
// Otherwise, perform a merge.
- ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0));
+ ConstantInt *DstBehavior =
+ mdconst::extract<ConstantInt>(DstOp->getOperand(0));
unsigned DstBehaviorValue = DstBehavior->getZExtValue();
// If either flag has override behavior, handle it first.
continue;
} else if (SrcBehaviorValue == Module::Override) {
// Update the destination flag to that of the source.
- DstOp->replaceOperandWith(0, SrcBehavior);
+ DstOp->replaceOperandWith(0, ConstantAsMetadata::get(SrcBehavior));
DstOp->replaceOperandWith(2, SrcOp->getOperand(2));
continue;
}
case Module::Append: {
MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
- unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands();
- Value **VP, **Values = VP = new Value*[NumOps];
- for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP)
- *VP = DstValue->getOperand(i);
- for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP)
- *VP = SrcValue->getOperand(i);
- DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
- ArrayRef<Value*>(Values,
- NumOps)));
- delete[] Values;
+ SmallVector<Metadata *, 8> MDs;
+ MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
+ for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
+ MDs.push_back(DstValue->getOperand(i));
+ for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
+ MDs.push_back(SrcValue->getOperand(i));
+ DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(), MDs));
break;
}
case Module::AppendUnique: {
- SmallSetVector<Value*, 16> Elts;
+ SmallSetVector<Metadata *, 16> Elts;
MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
Elts.insert(DstValue->getOperand(i));
for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
Elts.insert(SrcValue->getOperand(i));
- DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
- ArrayRef<Value*>(Elts.begin(),
- Elts.end())));
+ DstOp->replaceOperandWith(
+ 2, MDNode::get(DstM->getContext(),
+ makeArrayRef(Elts.begin(), Elts.end())));
break;
}
}
for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
MDNode *Requirement = Requirements[I];
MDString *Flag = cast<MDString>(Requirement->getOperand(0));
- Value *ReqValue = Requirement->getOperand(1);
+ Metadata *ReqValue = Requirement->getOperand(1);
MDNode *Op = Flags[Flag];
if (!Op || Op->getOperand(2) != ReqValue) {
linkGlobalValueBody(Src);
}
+ // Strip replaced subprograms before linking together compile units.
+ stripReplacedSubprograms();
+
// Remap all of the named MDNodes in Src into the DstM module. We do this
// after linking GlobalValues so that MDNodes that reference GlobalValues
// are properly remapped.
GlobalValue *SGV = LazilyLinkGlobalValues.back();
LazilyLinkGlobalValues.pop_back();
+ assert(!SGV->isDeclaration() && "users should not pass down decls");
if (linkGlobalValueBody(*SGV))
return true;
}
//===----------------------------------------------------------------------===//
LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
- LLVMLinkerMode Mode, char **OutMessages) {
+ unsigned Unused, char **OutMessages) {
Module *D = unwrap(Dest);
std::string Message;
raw_string_ostream Stream(Message);