llvm.org GIT mirror llvm / 3f329cb
Teach GVN to also propagate switch cases. For example, in this code switch (n) { case 27: do_something(x); ... } the call do_something(x) will be replaced with do_something(27). In gcc-as-one-big-file this results in the removal of about 500 lines of bitcode (about 0.02%), so has about 1/10 of the effect of propagating branch conditions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141360 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 8 years ago
2 changed file(s) with 96 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
19791979 return Changed;
19801980 }
19811981
1982 /// isOnlyReachableViaThisEdge - There is an edge from 'Src' to 'Dst'. Return
1983 /// true if every path from the entry block to 'Dst' passes via this edge. In
1984 /// particular 'Dst' must not be reachable via another edge from 'Src'.
1985 static bool isOnlyReachableViaThisEdge(BasicBlock *Src, BasicBlock *Dst,
1986 DominatorTree *DT) {
1987 // First off, there must not be more than one edge from Src to Dst, there
1988 // should be exactly one. So keep track of the number of times Src occurs
1989 // as a predecessor of Dst and fail if it's more than once. Secondly, any
1990 // other predecessors of Dst should be dominated by Dst (see logic below).
1991 bool SawEdgeFromSrc = false;
1992 for (pred_iterator PI = pred_begin(Dst), PE = pred_end(Dst); PI != PE; ++PI) {
1993 BasicBlock *Pred = *PI;
1994 if (Pred == Src) {
1995 // An edge from Src to Dst.
1996 if (SawEdgeFromSrc)
1997 // There are multiple edges from Src to Dst - fail.
1998 return false;
1999 SawEdgeFromSrc = true;
2000 continue;
2001 }
2002 // If the predecessor is not dominated by Dst, then it must be possible to
2003 // reach it either without passing through Src (and thus not via the edge)
2004 // or by passing through Src but taking a different edge out of Src. Either
2005 // way it is possible to reach Dst without passing via the edge, so fail.
2006 if (!DT->dominates(Dst, *PI))
2007 return false;
2008 }
2009 assert(SawEdgeFromSrc && "No edge between these basic blocks!");
2010
2011 // Every path from the entry block to Dst must at some point pass to Dst from
2012 // a predecessor that is not dominated by Dst. This predecessor can only be
2013 // Src, since all others are dominated by Dst. As there is only one edge from
2014 // Src to Dst, the path passes by this edge.
2015 return true;
2016 }
2017
19822018 /// processInstruction - When calculating availability, handle an instruction
19832019 /// by inserting it into the appropriate sets
19842020 bool GVN::processInstruction(Instruction *I) {
20102046
20112047 // For conditional branches, we can perform simple conditional propagation on
20122048 // the condition value itself.
2013 // TODO: Add conditional propagation of switch cases.
20142049 if (BranchInst *BI = dyn_cast(I)) {
20152050 if (!BI->isConditional() || isa(BI->getCondition()))
20162051 return false;
20202055 BasicBlock *TrueSucc = BI->getSuccessor(0);
20212056 BasicBlock *FalseSucc = BI->getSuccessor(1);
20222057 BasicBlock *Parent = BI->getParent();
2023
2024 // If the true and false branches are to the same basic block then the
2025 // branch gives no information about the condition. Eliminating this
2026 // here simplifies the rest of the logic.
2027 if (TrueSucc == FalseSucc)
2028 return false;
2029
2030 // If the true block can be reached without executing the true edge then we
2031 // can't say anything about the value of the condition there.
2032 for (pred_iterator PI = pred_begin(TrueSucc), PE = pred_end(TrueSucc);
2033 PI != PE; ++PI)
2034 if (*PI != Parent && !DT->dominates(TrueSucc, *PI)) {
2035 TrueSucc = 0;
2036 break;
2037 }
2038
2039 // If the false block can be reached without executing the false edge then
2040 // we can't say anything about the value of the condition there.
2041 for (pred_iterator PI = pred_begin(FalseSucc), PE = pred_end(FalseSucc);
2042 PI != PE; ++PI)
2043 if (*PI != Parent && !DT->dominates(FalseSucc, *PI)) {
2044 FalseSucc = 0;
2045 break;
2046 }
2047
2048 // Replace the condition with true/false in basic blocks that can only be
2049 // reached via the true/false arm of the branch.
2050 return (TrueSucc && propagateEquality(BranchCond,
2058 bool Changed = false;
2059
2060 if (isOnlyReachableViaThisEdge(Parent, TrueSucc, DT))
2061 Changed |= propagateEquality(BranchCond,
20512062 ConstantInt::getTrue(TrueSucc->getContext()),
2052 TrueSucc))
2053 || (FalseSucc && propagateEquality(BranchCond,
2054 ConstantInt::getFalse(FalseSucc->getContext()),
2055 FalseSucc));
2056 }
2057
2063 TrueSucc);
2064
2065 if (isOnlyReachableViaThisEdge(Parent, FalseSucc, DT))
2066 Changed |= propagateEquality(BranchCond,
2067 ConstantInt::getFalse(FalseSucc->getContext()),
2068 FalseSucc);
2069
2070 return Changed;
2071 }
2072
2073 // For switches, propagate the case values into the case destinations.
2074 if (SwitchInst *SI = dyn_cast(I)) {
2075 Value *SwitchCond = SI->getCondition();
2076 BasicBlock *Parent = SI->getParent();
2077 bool Changed = false;
2078 for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i) {
2079 BasicBlock *Dst = SI->getSuccessor(i);
2080 if (isOnlyReachableViaThisEdge(Parent, Dst, DT))
2081 Changed |= propagateEquality(SwitchCond, SI->getCaseValue(i), Dst);
2082 }
2083 return Changed;
2084 }
2085
20582086 // Instructions with void type don't return a value, so there's
20592087 // no point in trying to find redudancies in them.
20602088 if (I->getType()->isVoidTy()) return false;
9696 ; CHECK: call void @foo(i1 false)
9797 ret void
9898 }
99
100 ; CHECK: @test4
101 define void @test4(i1 %b, i32 %x) {
102 br i1 %b, label %sw, label %case3
103 sw:
104 switch i32 %x, label %default [
105 i32 0, label %case0
106 i32 1, label %case1
107 i32 2, label %case0
108 i32 3, label %case3
109 i32 4, label %default
110 ]
111 default:
112 ; CHECK: default:
113 call void @bar(i32 %x)
114 ; CHECK: call void @bar(i32 %x)
115 ret void
116 case0:
117 ; CHECK: case0:
118 call void @bar(i32 %x)
119 ; CHECK: call void @bar(i32 %x)
120 ret void
121 case1:
122 ; CHECK: case1:
123 call void @bar(i32 %x)
124 ; CHECK: call void @bar(i32 1)
125 ret void
126 case3:
127 ; CHECK: case3:
128 call void @bar(i32 %x)
129 ; CHECK: call void @bar(i32 %x)
130 ret void
131 }