@@ -23,107 +23,46 @@ using namespace llvm;
23
23
// / Removes all the Named and Unnamed Metadata Nodes, as well as any debug
24
24
// / functions that aren't inside the desired Chunks.
25
25
static void extractMetadataFromModule (Oracle &O, Module &Program) {
26
- SmallSetVector<MDNode *, 8 > NodesToVisit;
27
-
28
26
// Get out-of-chunk Named metadata nodes
29
27
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 ())
35
30
NamedNodesToDelete.push_back (&MD);
36
- }
37
- }
38
31
39
32
for (NamedMDNode *NN : NamedNodesToDelete) {
40
33
for (auto I : seq<unsigned >(0 , NN->getNumOperands ()))
41
34
NN->setOperand (I, nullptr );
42
35
NN->eraseFromParent ();
43
36
}
44
37
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
-
58
38
// Delete out-of-chunk metadata attached to globals.
59
39
for (GlobalVariable &GV : Program.globals ()) {
60
40
SmallVector<std::pair<unsigned , MDNode *>> MDs;
61
41
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 ())
66
44
GV.setMetadata (MD.first , nullptr );
67
- }
68
- }
69
45
}
70
46
71
47
for (Function &F : Program) {
72
48
{
73
49
SmallVector<std::pair<unsigned , MDNode *>> MDs;
74
50
// Delete out-of-chunk metadata attached to functions.
75
51
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 ())
80
54
F.setMetadata (MD.first , nullptr );
81
- }
82
- }
83
55
}
84
56
85
57
// Delete out-of-chunk metadata attached to instructions.
86
58
for (Instruction &I : instructions (F)) {
87
59
SmallVector<std::pair<unsigned , MDNode *>> MDs;
88
60
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 ())
93
63
I.setMetadata (MD.first , nullptr );
94
- }
95
- }
96
64
}
97
65
}
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
- }
127
66
}
128
67
129
68
void llvm::reduceMetadataDeltaPass (TestRunner &Test) {
0 commit comments