LLVM  4.0.0
DeadArgumentElimination.h
Go to the documentation of this file.
1 //===- DeadArgumentElimination.h - Eliminate Dead Args ----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass deletes dead arguments from internal functions. Dead argument
11 // elimination removes arguments which are directly dead, as well as arguments
12 // only passed into function calls as dead arguments of other functions. This
13 // pass also deletes dead return values in a similar way.
14 //
15 // This pass is often useful as a cleanup pass to run after aggressive
16 // interprocedural passes, which add possibly-dead arguments or return values.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
21 #define LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
22 
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/PassManager.h"
25 
26 #include <map>
27 #include <set>
28 #include <string>
29 
30 namespace llvm {
31 
32 /// Eliminate dead arguments (and return values) from functions.
34  : public PassInfoMixin<DeadArgumentEliminationPass> {
35 public:
36  /// Struct that represents (part of) either a return value or a function
37  /// argument. Used so that arguments and return values can be used
38  /// interchangeably.
39  struct RetOrArg {
40  RetOrArg(const Function *F, unsigned Idx, bool IsArg)
41  : F(F), Idx(Idx), IsArg(IsArg) {}
42  const Function *F;
43  unsigned Idx;
44  bool IsArg;
45 
46  /// Make RetOrArg comparable, so we can put it into a map.
47  bool operator<(const RetOrArg &O) const {
48  return std::tie(F, Idx, IsArg) < std::tie(O.F, O.Idx, O.IsArg);
49  }
50 
51  /// Make RetOrArg comparable, so we can easily iterate the multimap.
52  bool operator==(const RetOrArg &O) const {
53  return F == O.F && Idx == O.Idx && IsArg == O.IsArg;
54  }
55 
56  std::string getDescription() const {
57  return (Twine(IsArg ? "Argument #" : "Return value #") + Twine(Idx) +
58  " of function " + F->getName())
59  .str();
60  }
61  };
62 
63  /// Liveness enum - During our initial pass over the program, we determine
64  /// that things are either alive or maybe alive. We don't mark anything
65  /// explicitly dead (even if we know they are), since anything not alive
66  /// with no registered uses (in Uses) will never be marked alive and will
67  /// thus become dead in the end.
68  enum Liveness { Live, MaybeLive };
69 
70  /// Convenience wrapper
71  RetOrArg CreateRet(const Function *F, unsigned Idx) {
72  return RetOrArg(F, Idx, false);
73  }
74  /// Convenience wrapper
75  RetOrArg CreateArg(const Function *F, unsigned Idx) {
76  return RetOrArg(F, Idx, true);
77  }
78 
79  typedef std::multimap<RetOrArg, RetOrArg> UseMap;
80  /// This maps a return value or argument to any MaybeLive return values or
81  /// arguments it uses. This allows the MaybeLive values to be marked live
82  /// when any of its users is marked live.
83  /// For example (indices are left out for clarity):
84  /// - Uses[ret F] = ret G
85  /// This means that F calls G, and F returns the value returned by G.
86  /// - Uses[arg F] = ret G
87  /// This means that some function calls G and passes its result as an
88  /// argument to F.
89  /// - Uses[ret F] = arg F
90  /// This means that F returns one of its own arguments.
91  /// - Uses[arg F] = arg G
92  /// This means that G calls F and passes one of its own (G's) arguments
93  /// directly to F.
95 
96  typedef std::set<RetOrArg> LiveSet;
97  typedef std::set<const Function *> LiveFuncSet;
98 
99  /// This set contains all values that have been determined to be live.
101  /// This set contains all values that are cannot be changed in any way.
103 
105 
106  /// This allows this pass to do double-duty as the dead arg hacking pass
107  /// (used only by bugpoint).
108  bool ShouldHackArguments = false;
109 
110 public:
111  DeadArgumentEliminationPass(bool ShouldHackArguments_ = false)
112  : ShouldHackArguments(ShouldHackArguments_) {}
114 
115 private:
116  Liveness MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses);
117  Liveness SurveyUse(const Use *U, UseVector &MaybeLiveUses,
118  unsigned RetValNum = -1U);
119  Liveness SurveyUses(const Value *V, UseVector &MaybeLiveUses);
120 
121  void SurveyFunction(const Function &F);
122  void MarkValue(const RetOrArg &RA, Liveness L,
123  const UseVector &MaybeLiveUses);
124  void MarkLive(const RetOrArg &RA);
125  void MarkLive(const Function &F);
126  void PropagateLiveness(const RetOrArg &RA);
127  bool RemoveDeadStuffFromFunction(Function *F);
128  bool DeleteDeadVarargs(Function &Fn);
129  bool RemoveDeadArgumentsFromCallers(Function &Fn);
130 };
131 }
132 
133 #endif // LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
MachineLoop * L
Struct that represents (part of) either a return value or a function argument.
Eliminate dead arguments (and return values) from functions.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
RetOrArg CreateArg(const Function *F, unsigned Idx)
Convenience wrapper.
LiveSet LiveValues
This set contains all values that have been determined to be live.
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
#define F(x, y, z)
Definition: MD5.cpp:51
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:311
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
bool operator==(const RetOrArg &O) const
Make RetOrArg comparable, so we can easily iterate the multimap.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Module.h This file contains the declarations for the Module class.
UseMap Uses
This maps a return value or argument to any MaybeLive return values or arguments it uses...
LiveFuncSet LiveFunctions
This set contains all values that are cannot be changed in any way.
RetOrArg CreateRet(const Function *F, unsigned Idx)
Convenience wrapper.
std::set< const Function * > LiveFuncSet
bool ShouldHackArguments
This allows this pass to do double-duty as the dead arg hacking pass (used only by bugpoint)...
DeadArgumentEliminationPass(bool ShouldHackArguments_=false)
std::multimap< RetOrArg, RetOrArg > UseMap
RetOrArg(const Function *F, unsigned Idx, bool IsArg)
Liveness
Liveness enum - During our initial pass over the program, we determine that things are either alive o...
LLVM Value Representation.
Definition: Value.h:71
bool operator<(const RetOrArg &O) const
Make RetOrArg comparable, so we can put it into a map.
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)