ReduceInstructions.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===- ReduceArguments.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 Arguments from defined functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ReduceInstructions.h"
  14. using namespace llvm;
  15. /// Removes out-of-chunk arguments from functions, and modifies their calls
  16. /// accordingly. It also removes allocations of out-of-chunk arguments.
  17. static void extractInstrFromModule(Oracle &O, Module &Program) {
  18. std::vector<Instruction *> InitInstToKeep;
  19. for (auto &F : Program)
  20. for (auto &BB : F) {
  21. // Removing the terminator would make the block invalid. Only iterate over
  22. // instructions before the terminator.
  23. InitInstToKeep.push_back(BB.getTerminator());
  24. for (auto &Inst : make_range(BB.begin(), std::prev(BB.end())))
  25. if (O.shouldKeep())
  26. InitInstToKeep.push_back(&Inst);
  27. }
  28. // We create a vector first, then convert it to a set, so that we don't have
  29. // to pay the cost of rebalancing the set frequently if the order we insert
  30. // the elements doesn't match the order they should appear inside the set.
  31. std::set<Instruction *> InstToKeep(InitInstToKeep.begin(),
  32. InitInstToKeep.end());
  33. std::vector<Instruction *> InstToDelete;
  34. for (auto &F : Program)
  35. for (auto &BB : F)
  36. for (auto &Inst : BB)
  37. if (!InstToKeep.count(&Inst)) {
  38. Inst.replaceAllUsesWith(UndefValue::get(Inst.getType()));
  39. InstToDelete.push_back(&Inst);
  40. }
  41. for (auto &I : InstToDelete)
  42. I->eraseFromParent();
  43. }
  44. void llvm::reduceInstructionsDeltaPass(TestRunner &Test) {
  45. outs() << "*** Reducing Instructions...\n";
  46. runDeltaPass(Test, extractInstrFromModule);
  47. }