ReduceFunctions.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===- ReduceFunctions.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 functions (and any instruction that calls it) in the provided
  11. // Module.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ReduceFunctions.h"
  15. #include "Delta.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/IR/Instructions.h"
  18. #include <iterator>
  19. #include <vector>
  20. using namespace llvm;
  21. /// Removes all the Defined Functions
  22. /// that aren't inside any of the desired Chunks.
  23. static void extractFunctionsFromModule(Oracle &O, Module &Program) {
  24. // Record all out-of-chunk functions.
  25. std::vector<std::reference_wrapper<Function>> FuncsToRemove;
  26. copy_if(Program.functions(), std::back_inserter(FuncsToRemove),
  27. [&O](Function &F) {
  28. // Intrinsics don't have function bodies that are useful to
  29. // reduce. Additionally, intrinsics may have additional operand
  30. // constraints. But, do drop intrinsics that are not referenced.
  31. return (!F.isIntrinsic() || F.use_empty()) && !O.shouldKeep();
  32. });
  33. // Then, drop body of each of them. We want to batch this and do nothing else
  34. // here so that minimal number of remaining exteranal uses will remain.
  35. for (Function &F : FuncsToRemove)
  36. F.dropAllReferences();
  37. // And finally, we can actually delete them.
  38. for (Function &F : FuncsToRemove) {
  39. // Replace all *still* remaining uses with undef.
  40. F.replaceAllUsesWith(UndefValue::get(F.getType()));
  41. // And finally, fully drop it.
  42. F.eraseFromParent();
  43. }
  44. }
  45. void llvm::reduceFunctionsDeltaPass(TestRunner &Test) {
  46. errs() << "*** Reducing Functions...\n";
  47. runDeltaPass(Test, extractFunctionsFromModule);
  48. errs() << "----------------------------\n";
  49. }