}
// First build a map of the existing module flags and requirements.
- DenseMap<MDString*, MDNode*> Flags;
+ DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
SmallSetVector<MDNode*, 16> Requirements;
for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
MDNode *Op = DstModFlags->getOperand(I);
if (Behavior->getZExtValue() == Module::Require) {
Requirements.insert(cast<MDNode>(Op->getOperand(2)));
} else {
- Flags[ID] = Op;
+ Flags[ID] = std::make_pair(Op, I);
}
}
ConstantInt *SrcBehavior =
mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
MDString *ID = cast<MDString>(SrcOp->getOperand(1));
- MDNode *DstOp = Flags.lookup(ID);
+ MDNode *DstOp;
+ unsigned DstIndex;
+ std::tie(DstOp, DstIndex) = Flags.lookup(ID);
unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
// If this is a requirement, add it and continue.
// If there is no existing flag with this ID, just add it.
if (!DstOp) {
- Flags[ID] = SrcOp;
+ Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
DstModFlags->addOperand(SrcOp);
continue;
}
continue;
} else if (SrcBehaviorValue == Module::Override) {
// Update the destination flag to that of the source.
- DstOp->replaceOperandWith(0, ConstantAsMetadata::get(SrcBehavior));
- DstOp->replaceOperandWith(2, SrcOp->getOperand(2));
+ DstModFlags->setOperand(DstIndex, SrcOp);
+ Flags[ID].first = SrcOp;
continue;
}
continue;
}
+ auto replaceDstValue = [&](MDNode *New) {
+ Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
+ MDNode *Flag = MDNode::get(DstM->getContext(), FlagOps);
+ DstModFlags->setOperand(DstIndex, Flag);
+ Flags[ID].first = Flag;
+ };
+
// Perform the merge for standard behavior types.
switch (SrcBehaviorValue) {
case Module::Require:
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));
+
+ replaceDstValue(MDNode::get(DstM->getContext(), MDs));
break;
}
case Module::AppendUnique: {
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(),
- makeArrayRef(Elts.begin(), Elts.end())));
+
+ replaceDstValue(MDNode::get(DstM->getContext(),
+ makeArrayRef(Elts.begin(), Elts.end())));
break;
}
}
MDString *Flag = cast<MDString>(Requirement->getOperand(0));
Metadata *ReqValue = Requirement->getOperand(1);
- MDNode *Op = Flags[Flag];
+ MDNode *Op = Flags[Flag].first;
if (!Op || Op->getOperand(2) != ReqValue) {
HasErr |= emitError("linking module flags '" + Flag->getString() +
"': does not have the required value");
--- /dev/null
+; RUN: llvm-link %s %S/Inputs/module-flags-dont-change-others.ll -S -o - | FileCheck %s
+
+; Test that module-flag linking doesn't change other metadata. In particular,
+; !named should still point at the unmodified tuples (!3, !4, and !5) that
+; happen to also serve as module flags.
+
+; CHECK: !named = !{!0, !1, !2, !3, !4, !5}
+; CHECK: !llvm.module.flags = !{!6, !7, !8}
+!named = !{!0, !1, !2, !3, !4, !5}
+!llvm.module.flags = !{!3, !4, !5}
+
+; CHECK: !0 = !{}
+; CHECK: !1 = !{!0}
+; CHECK: !2 = !{!0, !1}
+; CHECK: !3 = !{i32 1, !"foo", i32 927}
+; CHECK: !4 = !{i32 5, !"bar", !0}
+; CHECK: !5 = !{i32 6, !"baz", !1}
+; CHECK: !6 = !{i32 4, !"foo", i32 37}
+; CHECK: !7 = !{i32 5, !"bar", !1}
+; CHECK: !8 = !{i32 6, !"baz", !2}
+!0 = !{}
+!1 = !{!0}
+!2 = !{!0, !1}
+!3 = !{i32 1, !"foo", i32 927}
+!4 = !{i32 5, !"bar", !0}
+!5 = !{i32 6, !"baz", !1}