ReduceFunctions.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "Utils.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SetVector.h"
  19. #include "llvm/Transforms/Utils/ModuleUtils.h"
  20. #include <iterator>
  21. #include <vector>
  22. using namespace llvm;
  23. /// Removes all the Defined Functions
  24. /// that aren't inside any of the desired Chunks.
  25. static void extractFunctionsFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
  26. Module &Program = WorkItem.getModule();
  27. // Record all out-of-chunk functions.
  28. SmallPtrSet<Constant *, 8> FuncsToRemove;
  29. for (Function &F : Program.functions()) {
  30. // Intrinsics don't have function bodies that are useful to
  31. // reduce. Additionally, intrinsics may have additional operand
  32. // constraints. But, do drop intrinsics that are not referenced.
  33. if ((!F.isIntrinsic() || F.use_empty()) && !hasAliasOrBlockAddressUse(F) &&
  34. !O.shouldKeep())
  35. FuncsToRemove.insert(&F);
  36. }
  37. removeFromUsedLists(Program, [&FuncsToRemove](Constant *C) {
  38. return FuncsToRemove.count(C);
  39. });
  40. // Then, drop body of each of them. We want to batch this and do nothing else
  41. // here so that minimal number of remaining exteranal uses will remain.
  42. for (Constant *F : FuncsToRemove)
  43. F->dropAllReferences();
  44. // And finally, we can actually delete them.
  45. for (Constant *F : FuncsToRemove) {
  46. // Replace all *still* remaining uses with the default value.
  47. F->replaceAllUsesWith(getDefaultValue(F->getType()));
  48. // And finally, fully drop it.
  49. cast<Function>(F)->eraseFromParent();
  50. }
  51. }
  52. void llvm::reduceFunctionsDeltaPass(TestRunner &Test) {
  53. runDeltaPass(Test, extractFunctionsFromModule, "Reducing Functions");
  54. }