DeadArgumentElimination.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DeadArgumentElimination.h - Eliminate Dead Args ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This pass deletes dead arguments from internal functions. Dead argument
  15. // elimination removes arguments which are directly dead, as well as arguments
  16. // only passed into function calls as dead arguments of other functions. This
  17. // pass also deletes dead return values in a similar way.
  18. //
  19. // This pass is often useful as a cleanup pass to run after aggressive
  20. // interprocedural passes, which add possibly-dead arguments or return values.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
  24. #define LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/ADT/Twine.h"
  27. #include "llvm/IR/Function.h"
  28. #include "llvm/IR/PassManager.h"
  29. #include <map>
  30. #include <set>
  31. #include <string>
  32. #include <tuple>
  33. namespace llvm {
  34. class Module;
  35. class Use;
  36. class Value;
  37. /// Eliminate dead arguments (and return values) from functions.
  38. class DeadArgumentEliminationPass
  39. : public PassInfoMixin<DeadArgumentEliminationPass> {
  40. public:
  41. /// Struct that represents (part of) either a return value or a function
  42. /// argument. Used so that arguments and return values can be used
  43. /// interchangeably.
  44. struct RetOrArg {
  45. const Function *F;
  46. unsigned Idx;
  47. bool IsArg;
  48. RetOrArg(const Function *F, unsigned Idx, bool IsArg)
  49. : F(F), Idx(Idx), IsArg(IsArg) {}
  50. /// Make RetOrArg comparable, so we can put it into a map.
  51. bool operator<(const RetOrArg &O) const {
  52. return std::tie(F, Idx, IsArg) < std::tie(O.F, O.Idx, O.IsArg);
  53. }
  54. /// Make RetOrArg comparable, so we can easily iterate the multimap.
  55. bool operator==(const RetOrArg &O) const {
  56. return F == O.F && Idx == O.Idx && IsArg == O.IsArg;
  57. }
  58. std::string getDescription() const {
  59. return (Twine(IsArg ? "Argument #" : "Return value #") + Twine(Idx) +
  60. " of function " + F->getName())
  61. .str();
  62. }
  63. };
  64. /// During our initial pass over the program, we determine that things are
  65. /// either alive or maybe alive. We don't mark anything explicitly dead (even
  66. /// if we know they are), since anything not alive with no registered uses
  67. /// (in Uses) will never be marked alive and will thus become dead in the end.
  68. enum Liveness { Live, MaybeLive };
  69. DeadArgumentEliminationPass(bool ShouldHackArguments = false)
  70. : ShouldHackArguments(ShouldHackArguments) {}
  71. PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
  72. /// Convenience wrapper
  73. RetOrArg createRet(const Function *F, unsigned Idx) {
  74. return RetOrArg(F, Idx, false);
  75. }
  76. /// Convenience wrapper
  77. RetOrArg createArg(const Function *F, unsigned Idx) {
  78. return RetOrArg(F, Idx, true);
  79. }
  80. using UseMap = std::multimap<RetOrArg, RetOrArg>;
  81. /// This maps a return value or argument to any MaybeLive return values or
  82. /// arguments it uses. This allows the MaybeLive values to be marked live
  83. /// when any of its users is marked live.
  84. /// For example (indices are left out for clarity):
  85. /// - Uses[ret F] = ret G
  86. /// This means that F calls G, and F returns the value returned by G.
  87. /// - Uses[arg F] = ret G
  88. /// This means that some function calls G and passes its result as an
  89. /// argument to F.
  90. /// - Uses[ret F] = arg F
  91. /// This means that F returns one of its own arguments.
  92. /// - Uses[arg F] = arg G
  93. /// This means that G calls F and passes one of its own (G's) arguments
  94. /// directly to F.
  95. UseMap Uses;
  96. using LiveSet = std::set<RetOrArg>;
  97. using LiveFuncSet = std::set<const Function *>;
  98. /// This set contains all values that have been determined to be live.
  99. LiveSet LiveValues;
  100. /// This set contains all values that are cannot be changed in any way.
  101. LiveFuncSet LiveFunctions;
  102. using UseVector = SmallVector<RetOrArg, 5>;
  103. /// This allows this pass to do double-duty as the dead arg hacking pass
  104. /// (used only by bugpoint).
  105. bool ShouldHackArguments = false;
  106. private:
  107. Liveness markIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses);
  108. Liveness surveyUse(const Use *U, UseVector &MaybeLiveUses,
  109. unsigned RetValNum = -1U);
  110. Liveness surveyUses(const Value *V, UseVector &MaybeLiveUses);
  111. void surveyFunction(const Function &F);
  112. bool isLive(const RetOrArg &RA);
  113. void markValue(const RetOrArg &RA, Liveness L,
  114. const UseVector &MaybeLiveUses);
  115. void markLive(const RetOrArg &RA);
  116. void markLive(const Function &F);
  117. void propagateLiveness(const RetOrArg &RA);
  118. bool removeDeadStuffFromFunction(Function *F);
  119. bool deleteDeadVarargs(Function &F);
  120. bool removeDeadArgumentsFromCallers(Function &F);
  121. };
  122. } // end namespace llvm
  123. #endif // LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
  124. #ifdef __GNUC__
  125. #pragma GCC diagnostic pop
  126. #endif