ReduceInstructions.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //===- ReduceInstructions.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 a function which calls the Generic Delta pass in order
  10. // to reduce uninteresting Instructions from defined functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ReduceInstructions.h"
  14. #include "Utils.h"
  15. #include "llvm/IR/Constants.h"
  16. using namespace llvm;
  17. /// Filter out cases where deleting the instruction will likely cause the
  18. /// user/def of the instruction to fail the verifier.
  19. //
  20. // TODO: Technically the verifier only enforces preallocated token usage and
  21. // there is a none token.
  22. static bool shouldAlwaysKeep(const Instruction &I) {
  23. return I.isEHPad() || I.getType()->isTokenTy() || I.isSwiftError();
  24. }
  25. /// Removes out-of-chunk arguments from functions, and modifies their calls
  26. /// accordingly. It also removes allocations of out-of-chunk arguments.
  27. static void extractInstrFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
  28. Module &Program = WorkItem.getModule();
  29. std::vector<Instruction *> InitInstToKeep;
  30. for (auto &F : Program)
  31. for (auto &BB : F) {
  32. // Removing the terminator would make the block invalid. Only iterate over
  33. // instructions before the terminator.
  34. InitInstToKeep.push_back(BB.getTerminator());
  35. for (auto &Inst : make_range(BB.begin(), std::prev(BB.end()))) {
  36. if (shouldAlwaysKeep(Inst) || O.shouldKeep())
  37. InitInstToKeep.push_back(&Inst);
  38. }
  39. }
  40. // We create a vector first, then convert it to a set, so that we don't have
  41. // to pay the cost of rebalancing the set frequently if the order we insert
  42. // the elements doesn't match the order they should appear inside the set.
  43. std::set<Instruction *> InstToKeep(InitInstToKeep.begin(),
  44. InitInstToKeep.end());
  45. std::vector<Instruction *> InstToDelete;
  46. for (auto &F : Program)
  47. for (auto &BB : F)
  48. for (auto &Inst : BB)
  49. if (!InstToKeep.count(&Inst)) {
  50. Inst.replaceAllUsesWith(getDefaultValue(Inst.getType()));
  51. InstToDelete.push_back(&Inst);
  52. }
  53. for (auto &I : InstToDelete)
  54. I->eraseFromParent();
  55. }
  56. void llvm::reduceInstructionsDeltaPass(TestRunner &Test) {
  57. runDeltaPass(Test, extractInstrFromModule, "Reducing Instructions");
  58. }