llvm.org GIT mirror llvm / 31e986b
Revert "[JumpThreading] Unfold selects that depend on the same condition" This reverts commit ac54d0066c478a09c7cd28d15d0f9ff8af984afc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286976 91177308-0d34-0410-b5e6-96231b3b80d8 Pablo Barrio 3 years ago
3 changed file(s) with 45 addition(s) and 131 deletion(s). Raw diff Collapse all Expand all
128128 BasicBlock *NewBB, BasicBlock *SuccBB);
129129 /// Check if the block has profile metadata for its outgoing edges.
130130 bool doesBlockHaveProfileData(BasicBlock *BB);
131 SelectInst *getSelectFedByPhi(PHINode *PN);
132 void expandSelect(SelectInst *SI);
133131 };
134132
135133 } // end namespace llvm
19621962 return false;
19631963 }
19641964
1965 /// GetSelectFedByPhi - Look for PHI/Select in the same BB of the form
1965 /// TryToUnfoldSelectInCurrBB - Look for PHI/Select in the same BB of the form
19661966 /// bb:
19671967 /// %p = phi [false, %bb1], [true, %bb2], [false, %bb3], [true, %bb4], ...
19681968 /// %s = select p, trueval, falseval
19691969 ///
1970 /// And return the select. Unfolding it into a branch structure later enables
1970 /// And expand the select into a branch structure. This later enables
19711971 /// jump-threading over bb in this pass.
19721972 ///
1973 /// Using the similar approach of SimplifyCFG::FoldCondBranchOnPHI(), return
1974 /// select if the associated PHI has at least one constant.
1975 SelectInst *JumpThreadingPass::getSelectFedByPhi(PHINode *PN) {
1976
1977 unsigned NumPHIValues = PN->getNumIncomingValues();
1978 if (NumPHIValues == 0 || !PN->hasOneUse())
1979 return nullptr;
1980
1981 SelectInst *SI = dyn_cast(PN->user_back());
1982 BasicBlock *BB = PN->getParent();
1983 if (!SI || SI->getParent() != BB)
1984 return nullptr;
1985
1986 Value *Cond = SI->getCondition();
1987 if (!Cond || Cond != PN || !Cond->getType()->isIntegerTy(1))
1988 return nullptr;
1989
1990 for (unsigned i = 0; i != NumPHIValues; ++i) {
1991 if (PN->getIncomingBlock(i) == BB)
1992 return nullptr;
1993 if (isa(PN->getIncomingValue(i)))
1994 return SI;
1995 }
1996
1997 return nullptr;
1998 }
1999
2000 /// ExpandSelect - Expand a select into an if-then-else construct.
2001 void JumpThreadingPass::expandSelect(SelectInst *SI) {
2002
2003 BasicBlock *BB = SI->getParent();
2004 TerminatorInst *Term =
2005 SplitBlockAndInsertIfThen(SI->getCondition(), SI, false);
2006 PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI);
2007 NewPN->addIncoming(SI->getTrueValue(), Term->getParent());
2008 NewPN->addIncoming(SI->getFalseValue(), BB);
2009 SI->replaceAllUsesWith(NewPN);
2010 SI->eraseFromParent();
2011 }
2012
2013 /// TryToUnfoldSelectInCurrBB - Unfold selects that could be jump-threaded were
2014 /// they if-then-elses. If the unfolded selects are not jump-threaded, it will
2015 /// be folded again in the later optimizations.
1973 /// Using the similar approach of SimplifyCFG::FoldCondBranchOnPHI(), unfold
1974 /// select if the associated PHI has at least one constant. If the unfolded
1975 /// select is not jump-threaded, it will be folded again in the later
1976 /// optimizations.
20161977 bool JumpThreadingPass::TryToUnfoldSelectInCurrBB(BasicBlock *BB) {
2017
20181978 // If threading this would thread across a loop header, don't thread the edge.
20191979 // See the comments above FindLoopHeaders for justifications and caveats.
20201980 if (LoopHeaders.count(BB))
20211981 return false;
20221982
2023 bool Changed = false;
2024 for (auto &I : *BB) {
2025
2026 // Look for a Phi/Select pair in the same basic block. The Phi feeds the
2027 // condition of the Select and at least one of the incoming values is a
2028 // constant.
2029 PHINode *PN;
2030 SelectInst *SI;
2031 if ((PN = dyn_cast(&I)) && (SI = getSelectFedByPhi(PN))) {
2032 expandSelect(SI);
2033 Changed = true;
1983 // Look for a Phi/Select pair in the same basic block. The Phi feeds the
1984 // condition of the Select and at least one of the incoming values is a
1985 // constant.
1986 for (BasicBlock::iterator BI = BB->begin();
1987 PHINode *PN = dyn_cast(BI); ++BI) {
1988 unsigned NumPHIValues = PN->getNumIncomingValues();
1989 if (NumPHIValues == 0 || !PN->hasOneUse())
20341990 continue;
2035 }
2036
2037 if (I.getType()->isIntegerTy(1)) {
2038
2039 SmallVector Selects;
2040
2041 // Look for scalar booleans used in selects as conditions. If there are
2042 // several selects that use the same boolean, they are candidates for jump
2043 // threading and therefore we should unfold them.
2044 for (Value *U : I.users())
2045 if (auto *SI = dyn_cast(U))
2046 Selects.push_back(SI);
2047 if (Selects.size() <= 1)
2048 continue;
2049
2050 // Remove duplicates
2051 std::sort(Selects.begin(), Selects.end());
2052 auto NewEnd = std::unique(Selects.begin(), Selects.end());
2053
2054 Changed = true;
2055 for (auto SI = Selects.begin(); SI != NewEnd; ++SI)
2056 expandSelect(*SI);
2057 }
2058 }
2059
2060 return Changed;
2061 }
1991
1992 SelectInst *SI = dyn_cast(PN->user_back());
1993 if (!SI || SI->getParent() != BB)
1994 continue;
1995
1996 Value *Cond = SI->getCondition();
1997 if (!Cond || Cond != PN || !Cond->getType()->isIntegerTy(1))
1998 continue;
1999
2000 bool HasConst = false;
2001 for (unsigned i = 0; i != NumPHIValues; ++i) {
2002 if (PN->getIncomingBlock(i) == BB)
2003 return false;
2004 if (isa(PN->getIncomingValue(i)))
2005 HasConst = true;
2006 }
2007
2008 if (HasConst) {
2009 // Expand the select.
2010 TerminatorInst *Term =
2011 SplitBlockAndInsertIfThen(SI->getCondition(), SI, false);
2012 PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI);
2013 NewPN->addIncoming(SI->getTrueValue(), Term->getParent());
2014 NewPN->addIncoming(SI->getFalseValue(), BB);
2015 SI->replaceAllUsesWith(NewPN);
2016 SI->eraseFromParent();
2017 return true;
2018 }
2019 }
2020
2021 return false;
2022 }
+0
-45
test/Transforms/JumpThreading/unfold-selects-same-cond.ll less more
None ; RUN: opt < %s -jump-threading -instcombine -simplifycfg -S | FileCheck %s
1
2 ; The three selects are jump-threaded so that instcombine can optimize, and
3 ; simplifycfg should turn the result into a single select.
4 define i32 @f(i32 %a, i32 %b) {
5 ; CHECK: select
6 ; CHECK-NOT: select
7 entry:
8 %0 = and i32 %a, 1
9 %1 = and i32 %b, 1
10 %xor = xor i32 %1, %a
11 %shr32 = lshr i32 %a, 1
12 %cmp10 = icmp eq i32 %xor, 1
13 %2 = xor i32 %b, 12345
14 %b.addr.1 = select i1 %cmp10, i32 %2, i32 %b
15 %shr1633 = lshr i32 %b.addr.1, 1
16 %3 = or i32 %shr1633, 54321
17 %b.addr.2 = select i1 %cmp10, i32 %3, i32 %shr1633
18 %shr1634 = lshr i32 %b.addr.2, 2
19 %4 = or i32 %shr1634, 54320
20 %b.addr.3 = select i1 %cmp10, i32 %4, i32 %shr1634
21 ret i32 %b.addr.3
22 }
23
24 ; Case where the condition is not only used as condition but also as the
25 ; true or false value in at least one of the selects.
26 define i1 @g(i32 %a, i32 %b) {
27 ; CHECK: select
28 ; CHECK-NOT: select
29 entry:
30 %0 = and i32 %a, 1
31 %1 = and i32 %b, 1
32 %xor = xor i32 %1, %a
33 %shr32 = lshr i32 %a, 1
34 %cmp10 = icmp eq i32 %xor, 1
35 %2 = xor i32 %b, 12345
36 %b.addr.1 = select i1 %cmp10, i32 %2, i32 %b
37 %shr1633 = lshr i32 %b.addr.1, 1
38 %3 = or i32 %shr1633, 54321
39 %b.addr.2 = select i1 %cmp10, i32 %3, i32 %shr1633
40 %shr1634 = lshr i32 %b.addr.2, 2
41 %4 = icmp eq i32 %shr1634, 54320
42 %b.addr.3 = select i1 %cmp10, i1 %4, i1 %cmp10
43 ret i1 %b.addr.3
44 }