DeadArgumentElimination.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /// Liveness enum - During our initial pass over the program, we determine
  65. /// that things are either alive or maybe alive. We don't mark anything
  66. /// explicitly dead (even if we know they are), since anything not alive
  67. /// with no registered uses (in Uses) will never be marked alive and will
  68. /// thus become dead in the end.
  69. enum Liveness { Live, MaybeLive };
  70. DeadArgumentEliminationPass(bool ShouldHackArguments_ = false)
  71. : ShouldHackArguments(ShouldHackArguments_) {}
  72. PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
  73. /// Convenience wrapper
  74. RetOrArg CreateRet(const Function *F, unsigned Idx) {
  75. return RetOrArg(F, Idx, false);
  76. }
  77. /// Convenience wrapper
  78. RetOrArg CreateArg(const Function *F, unsigned Idx) {
  79. return RetOrArg(F, Idx, true);
  80. }
  81. using UseMap = std::multimap<RetOrArg, RetOrArg>;
  82. /// This maps a return value or argument to any MaybeLive return values or
  83. /// arguments it uses. This allows the MaybeLive values to be marked live
  84. /// when any of its users is marked live.
  85. /// For example (indices are left out for clarity):
  86. /// - Uses[ret F] = ret G
  87. /// This means that F calls G, and F returns the value returned by G.
  88. /// - Uses[arg F] = ret G
  89. /// This means that some function calls G and passes its result as an
  90. /// argument to F.
  91. /// - Uses[ret F] = arg F
  92. /// This means that F returns one of its own arguments.
  93. /// - Uses[arg F] = arg G
  94. /// This means that G calls F and passes one of its own (G's) arguments
  95. /// directly to F.
  96. UseMap Uses;
  97. using LiveSet = std::set<RetOrArg>;
  98. using LiveFuncSet = std::set<const Function *>;
  99. /// This set contains all values that have been determined to be live.
  100. LiveSet LiveValues;
  101. /// This set contains all values that are cannot be changed in any way.
  102. LiveFuncSet LiveFunctions;
  103. using UseVector = SmallVector<RetOrArg, 5>;
  104. /// This allows this pass to do double-duty as the dead arg hacking pass
  105. /// (used only by bugpoint).
  106. bool ShouldHackArguments = false;
  107. private:
  108. Liveness MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses);
  109. Liveness SurveyUse(const Use *U, UseVector &MaybeLiveUses,
  110. unsigned RetValNum = -1U);
  111. Liveness SurveyUses(const Value *V, UseVector &MaybeLiveUses);
  112. void SurveyFunction(const Function &F);
  113. bool IsLive(const RetOrArg &RA);
  114. void MarkValue(const RetOrArg &RA, Liveness L,
  115. const UseVector &MaybeLiveUses);
  116. void MarkLive(const RetOrArg &RA);
  117. void MarkLive(const Function &F);
  118. void PropagateLiveness(const RetOrArg &RA);
  119. bool RemoveDeadStuffFromFunction(Function *F);
  120. bool DeleteDeadVarargs(Function &Fn);
  121. bool RemoveDeadArgumentsFromCallers(Function &Fn);
  122. };
  123. } // end namespace llvm
  124. #endif // LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
  125. #ifdef __GNUC__
  126. #pragma GCC diagnostic pop
  127. #endif