Skip to content

Commit c5fff90

Browse files
committed
[NFC][SimplifyCFG] Merge FoldTwoEntryPHINode() into it's only callee
1 parent 34a98e1 commit c5fff90

File tree

1 file changed

+72
-80
lines changed

1 file changed

+72
-80
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 72 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,17 +2765,84 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
27652765
return EverChanged;
27662766
}
27672767

2768-
/// Given a BB that starts with the specified two-entry PHI node,
2769-
/// see if we can eliminate it.
2770-
static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
2771-
DomTreeUpdater *DTU, const DataLayout &DL) {
2768+
static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
2769+
const TargetTransformInfo &TTI,
2770+
DomTreeUpdater *DTU,
2771+
const DataLayout &DL) {
2772+
assert(BI->isConditional() && !isa<ConstantInt>(BI->getCondition()) &&
2773+
BI->getSuccessor(0) != BI->getSuccessor(1) &&
2774+
"Only for truly conditional branches.");
2775+
BasicBlock *BB = BI->getParent();
2776+
2777+
// Which ones of our successors end up with an unconditional branch?
2778+
SmallVector<BasicBlock *, 2> UncondSuccessors;
2779+
SmallVector<BasicBlock *, 2> OtherSuccessors;
2780+
for (BasicBlock *Succ : successors(BI)) {
2781+
auto *SuccBI = dyn_cast<BranchInst>(Succ->getTerminator());
2782+
if (SuccBI && SuccBI->isUnconditional())
2783+
UncondSuccessors.emplace_back(Succ);
2784+
else
2785+
OtherSuccessors.emplace_back(Succ);
2786+
}
2787+
assert(UncondSuccessors.size() + OtherSuccessors.size() == 2 &&
2788+
"Can not have more than two successors!");
2789+
2790+
// If none do, then we can't do anything.
2791+
if (UncondSuccessors.empty())
2792+
return false;
2793+
2794+
// We want to hoist code from the unconditional block[s] and eliminate them,
2795+
// but if they have their address taken, then we essentially can't do this.
2796+
for (BasicBlock *UncondSucc : UncondSuccessors)
2797+
if (UncondSucc->hasAddressTaken())
2798+
return false;
2799+
2800+
// All unconditional successors must have a single (and the same) predecessor.
2801+
// FIXME: lift this restriction.
2802+
for (BasicBlock *UncondSucc : UncondSuccessors)
2803+
if (!UncondSucc->getSinglePredecessor())
2804+
return false;
2805+
2806+
// Now, what is the merge point?
2807+
BasicBlock *MergeBB = nullptr;
2808+
// If there was only a single unconditional successor,
2809+
// then the other successor *must* be the merge point.
2810+
if (UncondSuccessors.size() == 1)
2811+
MergeBB = OtherSuccessors.front();
2812+
2813+
// All unconditional successors must have the same successor themselves.
2814+
for (BasicBlock *UncondSucc : UncondSuccessors) {
2815+
auto *SuccBI = cast<BranchInst>(UncondSucc->getTerminator());
2816+
assert(SuccBI->isUnconditional() && "Should be an unconditional branch.");
2817+
BasicBlock *SuccOfSucc = SuccBI->getSuccessor(0);
2818+
if (!MergeBB) // First unconditional successor, record it's successor.
2819+
MergeBB = SuccOfSucc;
2820+
else if (SuccOfSucc != MergeBB) // Do all succs have the same successor?
2821+
return false;
2822+
}
2823+
2824+
assert(MergeBB && "Should have found the merge point.");
2825+
assert(all_of(UncondSuccessors,
2826+
[MergeBB](BasicBlock *UncondSucc) {
2827+
return is_contained(predecessors(MergeBB), UncondSucc);
2828+
}) &&
2829+
"All unconditional successors must be predecessors of merge block.");
2830+
assert((UncondSuccessors.size() != 1 ||
2831+
is_contained(predecessors(MergeBB), BB)) &&
2832+
"If there is only a single unconditional successor, then the dispatch "
2833+
"block must also be merge block's predecessor.");
2834+
2835+
auto *PN = dyn_cast<PHINode>(MergeBB->begin());
2836+
if (!PN || PN->getNumIncomingValues() != 2)
2837+
return false;
2838+
27722839
// Ok, this is a two entry PHI node. Check to see if this is a simple "if
27732840
// statement", which has a very simple dominance structure. Basically, we
27742841
// are trying to find the condition that is being branched on, which
27752842
// subsequently causes this merge to happen. We really want control
27762843
// dependence information for this check, but simplifycfg can't keep it up
27772844
// to date, and this catches most of the cases we care about anyway.
2778-
BasicBlock *MergeBB = PN->getParent();
2845+
MergeBB = PN->getParent();
27792846

27802847
BasicBlock *IfTrue, *IfFalse;
27812848
BranchInst *DomBI = GetIfCondition(MergeBB, IfTrue, IfFalse);
@@ -2961,81 +3028,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
29613028
return true;
29623029
}
29633030

2964-
static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
2965-
const TargetTransformInfo &TTI,
2966-
DomTreeUpdater *DTU,
2967-
const DataLayout &DL) {
2968-
assert(BI->isConditional() && !isa<ConstantInt>(BI->getCondition()) &&
2969-
BI->getSuccessor(0) != BI->getSuccessor(1) &&
2970-
"Only for truly conditional branches.");
2971-
BasicBlock *BB = BI->getParent();
2972-
2973-
// Which ones of our successors end up with an unconditional branch?
2974-
SmallVector<BasicBlock *, 2> UncondSuccessors;
2975-
SmallVector<BasicBlock *, 2> OtherSuccessors;
2976-
for (BasicBlock *Succ : successors(BI)) {
2977-
auto *SuccBI = dyn_cast<BranchInst>(Succ->getTerminator());
2978-
if (SuccBI && SuccBI->isUnconditional())
2979-
UncondSuccessors.emplace_back(Succ);
2980-
else
2981-
OtherSuccessors.emplace_back(Succ);
2982-
}
2983-
assert(UncondSuccessors.size() + OtherSuccessors.size() == 2 &&
2984-
"Can not have more than two successors!");
2985-
2986-
// If none do, then we can't do anything.
2987-
if (UncondSuccessors.empty())
2988-
return false;
2989-
2990-
// We want to hoist code from the unconditional block[s] and eliminate them,
2991-
// but if they have their address taken, then we essentially can't do this.
2992-
for (BasicBlock *UncondSucc : UncondSuccessors)
2993-
if (UncondSucc->hasAddressTaken())
2994-
return false;
2995-
2996-
// All unconditional successors must have a single (and the same) predecessor.
2997-
// FIXME: lift this restriction.
2998-
for (BasicBlock *UncondSucc : UncondSuccessors)
2999-
if (!UncondSucc->getSinglePredecessor())
3000-
return false;
3001-
3002-
// Now, what is the merge point?
3003-
BasicBlock *MergeBB = nullptr;
3004-
// If there was only a single unconditional successor,
3005-
// then the other successor *must* be the merge point.
3006-
if (UncondSuccessors.size() == 1)
3007-
MergeBB = OtherSuccessors.front();
3008-
3009-
// All unconditional successors must have the same successor themselves.
3010-
for (BasicBlock *UncondSucc : UncondSuccessors) {
3011-
auto *SuccBI = cast<BranchInst>(UncondSucc->getTerminator());
3012-
assert(SuccBI->isUnconditional() && "Should be an unconditional branch.");
3013-
BasicBlock *SuccOfSucc = SuccBI->getSuccessor(0);
3014-
if (!MergeBB) // First unconditional successor, record it's successor.
3015-
MergeBB = SuccOfSucc;
3016-
else if (SuccOfSucc != MergeBB) // Do all succs have the same successor?
3017-
return false;
3018-
}
3019-
3020-
assert(MergeBB && "Should have found the merge point.");
3021-
assert(all_of(UncondSuccessors,
3022-
[MergeBB](BasicBlock *UncondSucc) {
3023-
return is_contained(predecessors(MergeBB), UncondSucc);
3024-
}) &&
3025-
"All unconditional successors must be predecessors of merge block.");
3026-
assert((UncondSuccessors.size() != 1 ||
3027-
is_contained(predecessors(MergeBB), BB)) &&
3028-
"If there is only a single unconditional successor, then the dispatch "
3029-
"block must also be merge block's predecessor.");
3030-
3031-
if (auto *PN = dyn_cast<PHINode>(MergeBB->begin()))
3032-
// FIXME: lift this restriction.
3033-
if (PN->getNumIncomingValues() == 2)
3034-
return FoldTwoEntryPHINode(PN, TTI, DTU, DL);
3035-
3036-
return false;
3037-
}
3038-
30393031
static Value *createLogicalOp(IRBuilderBase &Builder,
30403032
Instruction::BinaryOps Opc, Value *LHS,
30413033
Value *RHS, const Twine &Name = "") {

0 commit comments

Comments
 (0)