ReduceMetadata.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "ReduceMetadata.h"
  14. #include "Delta.h"
  15. #include "llvm/ADT/Sequence.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/IR/InstIterator.h"
  18. #include <vector>
  19. using namespace llvm;
  20. /// Removes all the Named and Unnamed Metadata Nodes, as well as any debug
  21. /// functions that aren't inside the desired Chunks.
  22. static void extractMetadataFromModule(Oracle &O, Module &Program) {
  23. // Get out-of-chunk Named metadata nodes
  24. SmallVector<NamedMDNode *> NamedNodesToDelete;
  25. for (NamedMDNode &MD : Program.named_metadata())
  26. if (!O.shouldKeep())
  27. NamedNodesToDelete.push_back(&MD);
  28. for (NamedMDNode *NN : NamedNodesToDelete) {
  29. for (auto I : seq<unsigned>(0, NN->getNumOperands()))
  30. NN->setOperand(I, nullptr);
  31. NN->eraseFromParent();
  32. }
  33. // Delete out-of-chunk metadata attached to globals.
  34. for (GlobalVariable &GV : Program.globals()) {
  35. SmallVector<std::pair<unsigned, MDNode *>> MDs;
  36. GV.getAllMetadata(MDs);
  37. for (std::pair<unsigned, MDNode *> &MD : MDs)
  38. if (!O.shouldKeep())
  39. GV.setMetadata(MD.first, nullptr);
  40. }
  41. for (Function &F : Program) {
  42. {
  43. SmallVector<std::pair<unsigned, MDNode *>> MDs;
  44. // Delete out-of-chunk metadata attached to functions.
  45. F.getAllMetadata(MDs);
  46. for (std::pair<unsigned, MDNode *> &MD : MDs)
  47. if (!O.shouldKeep())
  48. F.setMetadata(MD.first, nullptr);
  49. }
  50. // Delete out-of-chunk metadata attached to instructions.
  51. for (Instruction &I : instructions(F)) {
  52. SmallVector<std::pair<unsigned, MDNode *>> MDs;
  53. I.getAllMetadata(MDs);
  54. for (std::pair<unsigned, MDNode *> &MD : MDs)
  55. if (!O.shouldKeep())
  56. I.setMetadata(MD.first, nullptr);
  57. }
  58. }
  59. }
  60. void llvm::reduceMetadataDeltaPass(TestRunner &Test) {
  61. outs() << "*** Reducing Metadata...\n";
  62. runDeltaPass(Test, extractMetadataFromModule);
  63. outs() << "----------------------------\n";
  64. }