1 //===- ReduceMetadata.cpp - Specialized Delta Pass ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements two functions used by the Generic Delta Debugging
10 // Algorithm, which are used to reduce Metadata nodes.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ReduceMetadata.h"
15 #include "Delta.h"
16 #include "llvm/ADT/Sequence.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/IR/InstIterator.h"
19 #include <vector>
20
21 using namespace llvm;
22
23 /// Removes all the Named and Unnamed Metadata Nodes, as well as any debug
24 /// functions that aren't inside the desired Chunks.
extractMetadataFromModule(Oracle & O,Module & Program)25 static void extractMetadataFromModule(Oracle &O, Module &Program) {
26 // Get out-of-chunk Named metadata nodes
27 SmallVector<NamedMDNode *> NamedNodesToDelete;
28 for (NamedMDNode &MD : Program.named_metadata())
29 if (!O.shouldKeep())
30 NamedNodesToDelete.push_back(&MD);
31
32 for (NamedMDNode *NN : NamedNodesToDelete) {
33 for (auto I : seq<unsigned>(0, NN->getNumOperands()))
34 NN->setOperand(I, nullptr);
35 NN->eraseFromParent();
36 }
37
38 // Delete out-of-chunk metadata attached to globals.
39 for (GlobalVariable &GV : Program.globals()) {
40 SmallVector<std::pair<unsigned, MDNode *>> MDs;
41 GV.getAllMetadata(MDs);
42 for (std::pair<unsigned, MDNode *> &MD : MDs)
43 if (!O.shouldKeep())
44 GV.setMetadata(MD.first, nullptr);
45 }
46
47 for (Function &F : Program) {
48 {
49 SmallVector<std::pair<unsigned, MDNode *>> MDs;
50 // Delete out-of-chunk metadata attached to functions.
51 F.getAllMetadata(MDs);
52 for (std::pair<unsigned, MDNode *> &MD : MDs)
53 if (!O.shouldKeep())
54 F.setMetadata(MD.first, nullptr);
55 }
56
57 // Delete out-of-chunk metadata attached to instructions.
58 for (Instruction &I : instructions(F)) {
59 SmallVector<std::pair<unsigned, MDNode *>> MDs;
60 I.getAllMetadata(MDs);
61 for (std::pair<unsigned, MDNode *> &MD : MDs)
62 if (!O.shouldKeep())
63 I.setMetadata(MD.first, nullptr);
64 }
65 }
66 }
67
reduceMetadataDeltaPass(TestRunner & Test)68 void llvm::reduceMetadataDeltaPass(TestRunner &Test) {
69 outs() << "*** Reducing Metadata...\n";
70 runDeltaPass(Test, extractMetadataFromModule);
71 outs() << "----------------------------\n";
72 }
73