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. 33 class DeadArgumentEliminationPass 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 { RetOrArgRetOrArg40 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 getDescriptionRetOrArg56 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 CreateRet(const Function * F,unsigned Idx)71 RetOrArg CreateRet(const Function *F, unsigned Idx) { 72 return RetOrArg(F, Idx, false); 73 } 74 /// Convenience wrapper CreateArg(const Function * F,unsigned Idx)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. 94 UseMap Uses; 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. 100 LiveSet LiveValues; 101 /// This set contains all values that are cannot be changed in any way. 102 LiveFuncSet LiveFunctions; 103 104 typedef SmallVector<RetOrArg, 5> UseVector; 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) ShouldHackArguments(ShouldHackArguments_)112 : ShouldHackArguments(ShouldHackArguments_) {} 113 PreservedAnalyses run(Module &M, ModuleAnalysisManager &); 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 134