@@ -23,107 +23,46 @@ using namespace llvm;
2323// / Removes all the Named and Unnamed Metadata Nodes, as well as any debug
2424// / functions that aren't inside the desired Chunks.
2525static void extractMetadataFromModule (Oracle &O, Module &Program) {
26- SmallSetVector<MDNode *, 8 > NodesToVisit;
27-
2826 // Get out-of-chunk Named metadata nodes
2927 SmallVector<NamedMDNode *> NamedNodesToDelete;
30- for (NamedMDNode &MD : Program.named_metadata ()) {
31- if (O.shouldKeep ()) {
32- for (auto *Op : MD.operands ())
33- NodesToVisit.insert (Op);
34- } else {
28+ for (NamedMDNode &MD : Program.named_metadata ())
29+ if (!O.shouldKeep ())
3530 NamedNodesToDelete.push_back (&MD);
36- }
37- }
3831
3932 for (NamedMDNode *NN : NamedNodesToDelete) {
4033 for (auto I : seq<unsigned >(0 , NN->getNumOperands ()))
4134 NN->setOperand (I, nullptr );
4235 NN->eraseFromParent ();
4336 }
4437
45- // Delete elements from named metadata lists
46- for (auto &NamedList : Program.named_metadata ()) {
47- SmallVector<MDNode *> NewOperands;
48- for (auto *Op : NamedList.operands ())
49- if (O.shouldKeep ())
50- NewOperands.push_back (Op);
51- if (NewOperands.size () == NamedList.getNumOperands ())
52- continue ;
53- NamedList.clearOperands ();
54- for (auto *Op : NewOperands)
55- NamedList.addOperand (Op);
56- }
57-
5838 // Delete out-of-chunk metadata attached to globals.
5939 for (GlobalVariable &GV : Program.globals ()) {
6040 SmallVector<std::pair<unsigned , MDNode *>> MDs;
6141 GV.getAllMetadata (MDs);
62- for (std::pair<unsigned , MDNode *> &MD : MDs) {
63- if (O.shouldKeep ()) {
64- NodesToVisit.insert (MD.second );
65- } else {
42+ for (std::pair<unsigned , MDNode *> &MD : MDs)
43+ if (!O.shouldKeep ())
6644 GV.setMetadata (MD.first , nullptr );
67- }
68- }
6945 }
7046
7147 for (Function &F : Program) {
7248 {
7349 SmallVector<std::pair<unsigned , MDNode *>> MDs;
7450 // Delete out-of-chunk metadata attached to functions.
7551 F.getAllMetadata (MDs);
76- for (std::pair<unsigned , MDNode *> &MD : MDs) {
77- if (O.shouldKeep ()) {
78- NodesToVisit.insert (MD.second );
79- } else {
52+ for (std::pair<unsigned , MDNode *> &MD : MDs)
53+ if (!O.shouldKeep ())
8054 F.setMetadata (MD.first , nullptr );
81- }
82- }
8355 }
8456
8557 // Delete out-of-chunk metadata attached to instructions.
8658 for (Instruction &I : instructions (F)) {
8759 SmallVector<std::pair<unsigned , MDNode *>> MDs;
8860 I.getAllMetadata (MDs);
89- for (std::pair<unsigned , MDNode *> &MD : MDs) {
90- if (O.shouldKeep ()) {
91- NodesToVisit.insert (MD.second );
92- } else {
61+ for (std::pair<unsigned , MDNode *> &MD : MDs)
62+ if (!O.shouldKeep ())
9363 I.setMetadata (MD.first , nullptr );
94- }
95- }
9664 }
9765 }
98-
99- // Gather all metadata tuples and their parents
100- SmallVector<std::pair<MDNode *, unsigned >> OperandsOfTuples;
101- SmallSet<Metadata *, 8 > VisitedNodes;
102- while (!NodesToVisit.empty ()) {
103- auto *Node = NodesToVisit.pop_back_val ();
104- if (!VisitedNodes.insert (Node).second )
105- continue ;
106- for (auto I : seq<unsigned >(0 , Node->getNumOperands ())) {
107- auto *Op = Node->getOperand (I).get ();
108- if (auto *MD = dyn_cast_or_null<MDNode>(Op))
109- NodesToVisit.insert (MD);
110- if (isa_and_nonnull<MDTuple>(Op))
111- OperandsOfTuples.push_back (std::make_pair (Node, I));
112- }
113- }
114-
115- // Delete elements from metadata tuples
116- for (auto [Node, NodeOpID] : OperandsOfTuples) {
117- auto *Tuple = dyn_cast<MDTuple>(Node->getOperand (NodeOpID));
118- SmallVector<Metadata *> NewOperands;
119- for (auto &Op : Tuple->operands ())
120- if (O.shouldKeep ())
121- NewOperands.push_back (Op.get ());
122- if (NewOperands.size () == Tuple->getNumOperands ())
123- continue ;
124- Node->replaceOperandWith (
125- NodeOpID, MDTuple::get (Tuple->getContext (), makeArrayRef (NewOperands)));
126- }
12766}
12867
12968void llvm::reduceMetadataDeltaPass (TestRunner &Test) {
0 commit comments