llvm.org GIT mirror llvm / 466d0a3
[PM] Port DeadArgumentElimination to the new PM The approach taken here follows r267631. deadarghaX0r should be easy to port when the time comes to add new-PM support to bugpoint. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272507 91177308-0d34-0410-b5e6-96231b3b80d8 Sean Silva 3 years ago
5 changed file(s) with 186 addition(s) and 122 deletion(s). Raw diff Collapse all Expand all
0 //===- DeadArgumentElimination.h - Eliminate Dead Args ----------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass deletes dead arguments from internal functions. Dead argument
10 // elimination removes arguments which are directly dead, as well as arguments
11 // only passed into function calls as dead arguments of other functions. This
12 // pass also deletes dead return values in a similar way.
13 //
14 // This pass is often useful as a cleanup pass to run after aggressive
15 // interprocedural passes, which add possibly-dead arguments or return values.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
20 #define LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
21
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/PassManager.h"
24
25 #include
26 #include
27 #include
28
29 namespace llvm {
30
31 /// Eliminate dead arguments (and return values) from functions.
32 class DeadArgumentEliminationPass
33 : public PassInfoMixin {
34 public:
35 /// Struct that represents (part of) either a return value or a function
36 /// argument. Used so that arguments and return values can be used
37 /// interchangeably.
38 struct RetOrArg {
39 RetOrArg(const Function *F, unsigned Idx, bool IsArg)
40 : F(F), Idx(Idx), IsArg(IsArg) {}
41 const Function *F;
42 unsigned Idx;
43 bool IsArg;
44
45 /// Make RetOrArg comparable, so we can put it into a map.
46 bool operator<(const RetOrArg &O) const {
47 return std::tie(F, Idx, IsArg) < std::tie(O.F, O.Idx, O.IsArg);
48 }
49
50 /// Make RetOrArg comparable, so we can easily iterate the multimap.
51 bool operator==(const RetOrArg &O) const {
52 return F == O.F && Idx == O.Idx && IsArg == O.IsArg;
53 }
54
55 std::string getDescription() const {
56 return (Twine(IsArg ? "Argument #" : "Return value #") + Twine(Idx) +
57 " of function " + F->getName())
58 .str();
59 }
60 };
61
62 /// Liveness enum - During our initial pass over the program, we determine
63 /// that things are either alive or maybe alive. We don't mark anything
64 /// explicitly dead (even if we know they are), since anything not alive
65 /// with no registered uses (in Uses) will never be marked alive and will
66 /// thus become dead in the end.
67 enum Liveness { Live, MaybeLive };
68
69 /// Convenience wrapper
70 RetOrArg CreateRet(const Function *F, unsigned Idx) {
71 return RetOrArg(F, Idx, false);
72 }
73 /// Convenience wrapper
74 RetOrArg CreateArg(const Function *F, unsigned Idx) {
75 return RetOrArg(F, Idx, true);
76 }
77
78 typedef std::multimap UseMap;
79 /// This maps a return value or argument to any MaybeLive return values or
80 /// arguments it uses. This allows the MaybeLive values to be marked live
81 /// when any of its users is marked live.
82 /// For example (indices are left out for clarity):
83 /// - Uses[ret F] = ret G
84 /// This means that F calls G, and F returns the value returned by G.
85 /// - Uses[arg F] = ret G
86 /// This means that some function calls G and passes its result as an
87 /// argument to F.
88 /// - Uses[ret F] = arg F
89 /// This means that F returns one of its own arguments.
90 /// - Uses[arg F] = arg G
91 /// This means that G calls F and passes one of its own (G's) arguments
92 /// directly to F.
93 UseMap Uses;
94
95 typedef std::set LiveSet;
96 typedef std::set LiveFuncSet;
97
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
103 typedef SmallVector UseVector;
104
105 /// This allows this pass to do double-duty as the dead arg hacking pass
106 /// (used only by bugpoint).
107 bool ShouldHackArguments = false;
108
109 public:
110 DeadArgumentEliminationPass(bool ShouldHackArguments_ = false)
111 : ShouldHackArguments(ShouldHackArguments_) {}
112 PreservedAnalyses run(Module &M);
113
114 private:
115 Liveness MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses);
116 Liveness SurveyUse(const Use *U, UseVector &MaybeLiveUses,
117 unsigned RetValNum = -1U);
118 Liveness SurveyUses(const Value *V, UseVector &MaybeLiveUses);
119
120 void SurveyFunction(const Function &F);
121 void MarkValue(const RetOrArg &RA, Liveness L,
122 const UseVector &MaybeLiveUses);
123 void MarkLive(const RetOrArg &RA);
124 void MarkLive(const Function &F);
125 void PropagateLiveness(const RetOrArg &RA);
126 bool RemoveDeadStuffFromFunction(Function *F);
127 bool DeleteDeadVarargs(Function &Fn);
128 bool RemoveDeadArgumentsFromCallers(Function &Fn);
129 };
130 }
131
132 #endif // LLVM_TRANSFORMS_IPO_DEADARGUMENTELIMINATION_H
5151 #include "llvm/Target/TargetMachine.h"
5252 #include "llvm/Transforms/GCOVProfiler.h"
5353 #include "llvm/Transforms/IPO/ConstantMerge.h"
54 #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
5455 #include "llvm/Transforms/IPO/ElimAvailExtern.h"
5556 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
5657 #include "llvm/Transforms/IPO/FunctionAttrs.h"
3737 #define MODULE_PASS(NAME, CREATE_PASS)
3838 #endif
3939 MODULE_PASS("constmerge", ConstantMergePass())
40 MODULE_PASS("deadargelim", DeadArgumentEliminationPass())
4041 MODULE_PASS("elim-avail-extern", EliminateAvailableExternallyPass())
4142 MODULE_PASS("forceattrs", ForceFunctionAttrsPass())
4243 MODULE_PASS("globaldce", GlobalDCEPass())
1616 //
1717 //===----------------------------------------------------------------------===//
1818
19 #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
1920 #include "llvm/ADT/SmallVector.h"
2021 #include "llvm/ADT/Statistic.h"
2122 #include "llvm/ADT/StringExtras.h"
4849 /// DAE - The dead argument elimination pass.
4950 ///
5051 class DAE : public ModulePass {
51 public:
52
53 /// Struct that represents (part of) either a return value or a function
54 /// argument. Used so that arguments and return values can be used
55 /// interchangeably.
56 struct RetOrArg {
57 RetOrArg(const Function *F, unsigned Idx, bool IsArg) : F(F), Idx(Idx),
58 IsArg(IsArg) {}
59 const Function *F;
60 unsigned Idx;
61 bool IsArg;
62
63 /// Make RetOrArg comparable, so we can put it into a map.
64 bool operator<(const RetOrArg &O) const {
65 return std::tie(F, Idx, IsArg) < std::tie(O.F, O.Idx, O.IsArg);
66 }
67
68 /// Make RetOrArg comparable, so we can easily iterate the multimap.
69 bool operator==(const RetOrArg &O) const {
70 return F == O.F && Idx == O.Idx && IsArg == O.IsArg;
71 }
72
73 std::string getDescription() const {
74 return (Twine(IsArg ? "Argument #" : "Return value #") + Twine(Idx) +
75 " of function " + F->getName()).str();
76 }
77 };
78
79 /// Liveness enum - During our initial pass over the program, we determine
80 /// that things are either alive or maybe alive. We don't mark anything
81 /// explicitly dead (even if we know they are), since anything not alive
82 /// with no registered uses (in Uses) will never be marked alive and will
83 /// thus become dead in the end.
84 enum Liveness { Live, MaybeLive };
85
86 /// Convenience wrapper
87 RetOrArg CreateRet(const Function *F, unsigned Idx) {
88 return RetOrArg(F, Idx, false);
89 }
90 /// Convenience wrapper
91 RetOrArg CreateArg(const Function *F, unsigned Idx) {
92 return RetOrArg(F, Idx, true);
93 }
94
95 typedef std::multimap UseMap;
96 /// This maps a return value or argument to any MaybeLive return values or
97 /// arguments it uses. This allows the MaybeLive values to be marked live
98 /// when any of its users is marked live.
99 /// For example (indices are left out for clarity):
100 /// - Uses[ret F] = ret G
101 /// This means that F calls G, and F returns the value returned by G.
102 /// - Uses[arg F] = ret G
103 /// This means that some function calls G and passes its result as an
104 /// argument to F.
105 /// - Uses[ret F] = arg F
106 /// This means that F returns one of its own arguments.
107 /// - Uses[arg F] = arg G
108 /// This means that G calls F and passes one of its own (G's) arguments
109 /// directly to F.
110 UseMap Uses;
111
112 typedef std::set LiveSet;
113 typedef std::set LiveFuncSet;
114
115 /// This set contains all values that have been determined to be live.
116 LiveSet LiveValues;
117 /// This set contains all values that are cannot be changed in any way.
118 LiveFuncSet LiveFunctions;
119
120 typedef SmallVector UseVector;
121
12252 protected:
12353 // DAH uses this to specify a different ID.
12454 explicit DAE(char &ID) : ModulePass(ID) {}
12959 initializeDAEPass(*PassRegistry::getPassRegistry());
13060 }
13161
132 bool runOnModule(Module &M) override;
62 bool runOnModule(Module &M) override {
63 if (skipModule(M))
64 return false;
65 DeadArgumentEliminationPass DAEP(ShouldHackArguments());
66 PreservedAnalyses PA = DAEP.run(M);
67 return !PA.areAllPreserved();
68 }
13369
13470 virtual bool ShouldHackArguments() const { return false; }
135
136 private:
137 Liveness MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses);
138 Liveness SurveyUse(const Use *U, UseVector &MaybeLiveUses,
139 unsigned RetValNum = -1U);
140 Liveness SurveyUses(const Value *V, UseVector &MaybeLiveUses);
141
142 void SurveyFunction(const Function &F);
143 void MarkValue(const RetOrArg &RA, Liveness L,
144 const UseVector &MaybeLiveUses);
145 void MarkLive(const RetOrArg &RA);
146 void MarkLive(const Function &F);
147 void PropagateLiveness(const RetOrArg &RA);
148 bool RemoveDeadStuffFromFunction(Function *F);
149 bool DeleteDeadVarargs(Function &Fn);
150 bool RemoveDeadArgumentsFromCallers(Function &Fn);
15171 };
15272 }
15373
180100
181101 /// DeleteDeadVarargs - If this is an function that takes a ... list, and if
182102 /// llvm.vastart is never called, the varargs list is dead for the function.
183 bool DAE::DeleteDeadVarargs(Function &Fn) {
103 bool DeadArgumentEliminationPass::DeleteDeadVarargs(Function &Fn) {
184104 assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!");
185105 if (Fn.isDeclaration() || !Fn.hasLocalLinkage()) return false;
186106
316236 /// RemoveDeadArgumentsFromCallers - Checks if the given function has any
317237 /// arguments that are unused, and changes the caller parameters to be undefined
318238 /// instead.
319 bool DAE::RemoveDeadArgumentsFromCallers(Function &Fn)
320 {
239 bool DeadArgumentEliminationPass::RemoveDeadArgumentsFromCallers(Function &Fn) {
321240 // We cannot change the arguments if this TU does not define the function or
322241 // if the linker may choose a function body from another TU, even if the
323242 // nominal linkage indicates that other copies of the function have the same
409328 /// MarkIfNotLive - This checks Use for liveness in LiveValues. If Use is not
410329 /// live, it adds Use to the MaybeLiveUses argument. Returns the determined
411330 /// liveness of Use.
412 DAE::Liveness DAE::MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses) {
331 DeadArgumentEliminationPass::Liveness
332 DeadArgumentEliminationPass::MarkIfNotLive(RetOrArg Use,
333 UseVector &MaybeLiveUses) {
413334 // We're live if our use or its Function is already marked as live.
414335 if (LiveFunctions.count(Use.F) || LiveValues.count(Use))
415336 return Live;
428349 /// RetValNum is the return value number to use when this use is used in a
429350 /// return instruction. This is used in the recursion, you should always leave
430351 /// it at 0.
431 DAE::Liveness DAE::SurveyUse(const Use *U,
432 UseVector &MaybeLiveUses, unsigned RetValNum) {
352 DeadArgumentEliminationPass::Liveness
353 DeadArgumentEliminationPass::SurveyUse(const Use *U, UseVector &MaybeLiveUses,
354 unsigned RetValNum) {
433355 const User *V = U->getUser();
434356 if (const ReturnInst *RI = dyn_cast(V)) {
435357 // The value is returned from a function. It's only live when the
442364 // We might be live, depending on the liveness of Use.
443365 return MarkIfNotLive(Use, MaybeLiveUses);
444366 } else {
445 DAE::Liveness Result = MaybeLive;
367 DeadArgumentEliminationPass::Liveness Result = MaybeLive;
446368 for (unsigned i = 0; i < NumRetVals(F); ++i) {
447369 RetOrArg Use = CreateRet(F, i);
448370 // We might be live, depending on the liveness of Use. If any
449371 // sub-value is live, then the entire value is considered live. This
450372 // is a conservative choice, and better tracking is possible.
451 DAE::Liveness SubResult = MarkIfNotLive(Use, MaybeLiveUses);
373 DeadArgumentEliminationPass::Liveness SubResult =
374 MarkIfNotLive(Use, MaybeLiveUses);
452375 if (Result != Live)
453376 Result = SubResult;
454377 }
514437 /// Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses. If
515438 /// the result is Live, MaybeLiveUses might be modified but its content should
516439 /// be ignored (since it might not be complete).
517 DAE::Liveness DAE::SurveyUses(const Value *V, UseVector &MaybeLiveUses) {
440 DeadArgumentEliminationPass::Liveness
441 DeadArgumentEliminationPass::SurveyUses(const Value *V,
442 UseVector &MaybeLiveUses) {
518443 // Assume it's dead (which will only hold if there are no uses at all..).
519444 Liveness Result = MaybeLive;
520445 // Check each use.
534459 // We consider arguments of non-internal functions to be intrinsically alive as
535460 // well as arguments to functions which have their "address taken".
536461 //
537 void DAE::SurveyFunction(const Function &F) {
462 void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
538463 // Functions with inalloca parameters are expecting args in a particular
539464 // register and memory layout.
540465 if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca)) {
570495 return;
571496 }
572497
573 if (!F.hasLocalLinkage() && (!ShouldHackArguments() || F.isIntrinsic())) {
498 if (!F.hasLocalLinkage() && (!ShouldHackArguments || F.isIntrinsic())) {
574499 MarkLive(F);
575500 return;
576501 }
577502
578 DEBUG(dbgs() << "DAE - Inspecting callers for fn: " << F.getName() << "\n");
503 DEBUG(dbgs() << "DeadArgumentEliminationPass - Inspecting callers for fn: "
504 << F.getName() << "\n");
579505 // Keep track of the number of live retvals, so we can skip checks once all
580506 // of them turn out to be live.
581507 unsigned NumLiveRetVals = 0;
637563 for (unsigned i = 0; i != RetCount; ++i)
638564 MarkValue(CreateRet(&F, i), RetValLiveness[i], MaybeLiveRetUses[i]);
639565
640 DEBUG(dbgs() << "DAE - Inspecting args for fn: " << F.getName() << "\n");
566 DEBUG(dbgs() << "DeadArgumentEliminationPass - Inspecting args for fn: "
567 << F.getName() << "\n");
641568
642569 // Now, check all of our arguments.
643570 unsigned i = 0;
669596 /// MaybeLive, it also takes all uses in MaybeLiveUses and records them in Uses,
670597 /// such that RA will be marked live if any use in MaybeLiveUses gets marked
671598 /// live later on.
672 void DAE::MarkValue(const RetOrArg &RA, Liveness L,
673 const UseVector &MaybeLiveUses) {
599 void DeadArgumentEliminationPass::MarkValue(const RetOrArg &RA, Liveness L,
600 const UseVector &MaybeLiveUses) {
674601 switch (L) {
675602 case Live: MarkLive(RA); break;
676603 case MaybeLive:
689616 /// changed in any way. Additionally,
690617 /// mark any values that are used as this function's parameters or by its return
691618 /// values (according to Uses) live as well.
692 void DAE::MarkLive(const Function &F) {
693 DEBUG(dbgs() << "DAE - Intrinsically live fn: " << F.getName() << "\n");
619 void DeadArgumentEliminationPass::MarkLive(const Function &F) {
620 DEBUG(dbgs() << "DeadArgumentEliminationPass - Intrinsically live fn: "
621 << F.getName() << "\n");
694622 // Mark the function as live.
695623 LiveFunctions.insert(&F);
696624 // Mark all arguments as live.
704632 /// MarkLive - Mark the given return value or argument as live. Additionally,
705633 /// mark any values that are used by this value (according to Uses) live as
706634 /// well.
707 void DAE::MarkLive(const RetOrArg &RA) {
635 void DeadArgumentEliminationPass::MarkLive(const RetOrArg &RA) {
708636 if (LiveFunctions.count(RA.F))
709637 return; // Function was already marked Live.
710638
711639 if (!LiveValues.insert(RA).second)
712640 return; // We were already marked Live.
713641
714 DEBUG(dbgs() << "DAE - Marking " << RA.getDescription() << " live\n");
642 DEBUG(dbgs() << "DeadArgumentEliminationPass - Marking "
643 << RA.getDescription() << " live\n");
715644 PropagateLiveness(RA);
716645 }
717646
718647 /// PropagateLiveness - Given that RA is a live value, propagate it's liveness
719648 /// to any other values it uses (according to Uses).
720 void DAE::PropagateLiveness(const RetOrArg &RA) {
649 void DeadArgumentEliminationPass::PropagateLiveness(const RetOrArg &RA) {
721650 // We don't use upper_bound (or equal_range) here, because our recursive call
722651 // to ourselves is likely to cause the upper_bound (which is the first value
723652 // not belonging to RA) to become erased and the iterator invalidated.
736665 // that are not in LiveValues. Transform the function and all of the callees of
737666 // the function to not have these arguments and return values.
738667 //
739 bool DAE::RemoveDeadStuffFromFunction(Function *F) {
668 bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {
740669 // Don't modify fully live functions
741670 if (LiveFunctions.count(F))
742671 return false;
777706 }
778707 } else {
779708 ++NumArgumentsEliminated;
780 DEBUG(dbgs() << "DAE - Removing argument " << i << " (" << I->getName()
781 << ") from " << F->getName() << "\n");
709 DEBUG(dbgs() << "DeadArgumentEliminationPass - Removing argument " << i
710 << " (" << I->getName() << ") from " << F->getName()
711 << "\n");
782712 }
783713 }
784714
821751 NewRetIdxs[i] = RetTypes.size() - 1;
822752 } else {
823753 ++NumRetValsEliminated;
824 DEBUG(dbgs() << "DAE - Removing return value " << i << " from "
825 << F->getName() << "\n");
754 DEBUG(dbgs() << "DeadArgumentEliminationPass - Removing return value "
755 << i << " from " << F->getName() << "\n");
826756 }
827757 }
828758 if (RetTypes.size() > 1) {
10961026 return true;
10971027 }
10981028
1099 bool DAE::runOnModule(Module &M) {
1100 if (skipModule(M))
1101 return false;
1102
1029 PreservedAnalyses DeadArgumentEliminationPass::run(Module &M) {
11031030 bool Changed = false;
11041031
11051032 // First pass: Do a simple check to see if any functions can have their "..."
11061033 // removed. We can do this if they never call va_start. This loop cannot be
11071034 // fused with the next loop, because deleting a function invalidates
11081035 // information computed while surveying other functions.
1109 DEBUG(dbgs() << "DAE - Deleting dead varargs\n");
1036 DEBUG(dbgs() << "DeadArgumentEliminationPass - Deleting dead varargs\n");
11101037 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
11111038 Function &F = *I++;
11121039 if (F.getFunctionType()->isVarArg())
11171044 // We assume all arguments are dead unless proven otherwise (allowing us to
11181045 // determine that dead arguments passed into recursive functions are dead).
11191046 //
1120 DEBUG(dbgs() << "DAE - Determining liveness\n");
1047 DEBUG(dbgs() << "DeadArgumentEliminationPass - Determining liveness\n");
11211048 for (auto &F : M)
11221049 SurveyFunction(F);
11231050
11351062 for (auto &F : M)
11361063 Changed |= RemoveDeadArgumentsFromCallers(F);
11371064
1138 return Changed;
1139 }
1065 if (!Changed)
1066 return PreservedAnalyses::all();
1067 return PreservedAnalyses::none();
1068 }
None ; RUN: opt < %s -deadargelim -S | FileCheck %s
0 ; RUN: opt < %s -passes=deadargelim -S | FileCheck %s
11
22 %Ty = type { i32, i32 }
33