Skip to content

Commit ee4ba9f

Browse files
committed
Revert "[SimplifyCFG] Start redesigning FoldTwoEntryPHINode()."
Unfortunately, it seems we really do need to take the long route; start from the "merge" block, find (all the) "dispatch" blocks, and deal with each "dispatch" block separately, instead of simply starting from each "dispatch" block like it would logically make sense, otherwise we run into a number of other missing folds around `switch` formation, missing sinking/hoisting and phase ordering. This reverts commit 85628ce. This reverts commit c5fff90. This reverts commit 34a98e1. This reverts commit 1e353f0.
1 parent 01bfe97 commit ee4ba9f

15 files changed

+303
-391
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

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

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

28462780
BasicBlock *IfTrue, *IfFalse;
2847-
BranchInst *DomBI = GetIfCondition(MergeBB, IfTrue, IfFalse);
2781+
BranchInst *DomBI = GetIfCondition(BB, IfTrue, IfFalse);
28482782
if (!DomBI)
28492783
return false;
28502784
Value *IfCond = DomBI->getCondition();
@@ -2876,7 +2810,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
28762810
BranchProbability BIFalseProb = BITrueProb.getCompl();
28772811
if (IfBlocks.size() == 1) {
28782812
BranchProbability BIBBProb =
2879-
DomBI->getSuccessor(0) == MergeBB ? BITrueProb : BIFalseProb;
2813+
DomBI->getSuccessor(0) == BB ? BITrueProb : BIFalseProb;
28802814
if (BIBBProb >= Likely)
28812815
return false;
28822816
} else {
@@ -2889,7 +2823,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
28892823
// Don't try to fold an unreachable block. For example, the phi node itself
28902824
// can't be the candidate if-condition for a select that we want to form.
28912825
if (auto *IfCondPhiInst = dyn_cast<PHINode>(IfCond))
2892-
if (IfCondPhiInst->getParent() == MergeBB)
2826+
if (IfCondPhiInst->getParent() == BB)
28932827
return false;
28942828

28952829
// Okay, we found that we can merge this two-entry phi node into a select.
@@ -2898,8 +2832,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
28982832
// doesn't support cmov's). Only do this transformation if there are two or
28992833
// fewer PHI nodes in this block.
29002834
unsigned NumPhis = 0;
2901-
for (BasicBlock::iterator I = MergeBB->begin(); isa<PHINode>(I);
2902-
++NumPhis, ++I)
2835+
for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
29032836
if (NumPhis > 2)
29042837
return false;
29052838

@@ -2912,7 +2845,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
29122845
TwoEntryPHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
29132846

29142847
bool Changed = false;
2915-
for (BasicBlock::iterator II = MergeBB->begin(); isa<PHINode>(II);) {
2848+
for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) {
29162849
PHINode *PN = cast<PHINode>(II++);
29172850
if (Value *V = SimplifyInstruction(PN, {DL, PN})) {
29182851
PN->replaceAllUsesWith(V);
@@ -2921,16 +2854,16 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
29212854
continue;
29222855
}
29232856

2924-
if (!dominatesMergePoint(PN->getIncomingValue(0), MergeBB, AggressiveInsts,
2857+
if (!dominatesMergePoint(PN->getIncomingValue(0), BB, AggressiveInsts,
29252858
Cost, Budget, TTI) ||
2926-
!dominatesMergePoint(PN->getIncomingValue(1), MergeBB, AggressiveInsts,
2859+
!dominatesMergePoint(PN->getIncomingValue(1), BB, AggressiveInsts,
29272860
Cost, Budget, TTI))
29282861
return Changed;
29292862
}
29302863

29312864
// If we folded the first phi, PN dangles at this point. Refresh it. If
29322865
// we ran out of PHIs then we simplified them all.
2933-
PN = dyn_cast<PHINode>(MergeBB->begin());
2866+
PN = dyn_cast<PHINode>(BB->begin());
29342867
if (!PN)
29352868
return true;
29362869

@@ -2994,7 +2927,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
29942927
IRBuilder<NoFolder> Builder(DomBI);
29952928
// Propagate fast-math-flags from phi nodes to replacement selects.
29962929
IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
2997-
while (PHINode *PN = dyn_cast<PHINode>(MergeBB->begin())) {
2930+
while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
29982931
if (isa<FPMathOperator>(PN))
29992932
Builder.setFastMathFlags(PN->getFastMathFlags());
30002933

@@ -3011,11 +2944,11 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
30112944
// At this point, all IfBlocks are empty, so our if statement
30122945
// has been flattened. Change DomBlock to jump directly to our new block to
30132946
// avoid other simplifycfg's kicking in on the diamond.
3014-
Builder.CreateBr(MergeBB);
2947+
Builder.CreateBr(BB);
30152948

30162949
SmallVector<DominatorTree::UpdateType, 3> Updates;
30172950
if (DTU) {
3018-
Updates.push_back({DominatorTree::Insert, DomBlock, MergeBB});
2951+
Updates.push_back({DominatorTree::Insert, DomBlock, BB});
30192952
for (auto *Successor : successors(DomBlock))
30202953
Updates.push_back({DominatorTree::Delete, DomBlock, Successor});
30212954
}
@@ -6588,11 +6521,6 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
65886521
return requestResimplify();
65896522
}
65906523

6591-
if (Options.FoldTwoEntryPHINode) {
6592-
if (SpeculativelyExecuteThenElseCode(BI, TTI, DTU, DL))
6593-
return true;
6594-
}
6595-
65966524
// If this is a branch on a phi node in the current block, thread control
65976525
// through this block if any PHI node entries are constants.
65986526
if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
@@ -6808,6 +6736,15 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
68086736

68096737
IRBuilder<> Builder(BB);
68106738

6739+
if (Options.FoldTwoEntryPHINode) {
6740+
// If there is a trivial two-entry PHI node in this basic block, and we can
6741+
// eliminate it, do so now.
6742+
if (auto *PN = dyn_cast<PHINode>(BB->begin()))
6743+
if (PN->getNumIncomingValues() == 2)
6744+
if (FoldTwoEntryPHINode(PN, TTI, DTU, DL))
6745+
return true;
6746+
}
6747+
68116748
Instruction *Terminator = BB->getTerminator();
68126749
Builder.SetInsertPoint(Terminator);
68136750
switch (Terminator->getOpcode()) {

llvm/test/CodeGen/AArch64/check-sign-bit-before-extension.ll

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
define i32 @f_i8_sign_extend_inreg(i8 %in, i32 %a, i32 %b) nounwind {
1414
; CHECK-LABEL: f_i8_sign_extend_inreg:
1515
; CHECK: // %bb.0: // %entry
16-
; CHECK-NEXT: and w8, w0, #0xff
17-
; CHECK-NEXT: sxtb w9, w0
18-
; CHECK-NEXT: add w10, w8, w1
19-
; CHECK-NEXT: add w8, w8, w2
20-
; CHECK-NEXT: cmp w9, #0
21-
; CHECK-NEXT: csel w0, w10, w8, ge
16+
; CHECK-NEXT: sxtb w8, w0
17+
; CHECK-NEXT: cmp w8, #0
18+
; CHECK-NEXT: csel w8, w1, w2, ge
19+
; CHECK-NEXT: add w0, w8, w0, uxtb
2220
; CHECK-NEXT: ret
2321
entry:
2422
%cmp = icmp sgt i8 %in, -1
@@ -37,12 +35,10 @@ B:
3735
define i32 @f_i16_sign_extend_inreg(i16 %in, i32 %a, i32 %b) nounwind {
3836
; CHECK-LABEL: f_i16_sign_extend_inreg:
3937
; CHECK: // %bb.0: // %entry
40-
; CHECK-NEXT: and w8, w0, #0xffff
41-
; CHECK-NEXT: sxth w9, w0
42-
; CHECK-NEXT: add w10, w8, w1
43-
; CHECK-NEXT: add w8, w8, w2
44-
; CHECK-NEXT: cmp w9, #0
45-
; CHECK-NEXT: csel w0, w10, w8, ge
38+
; CHECK-NEXT: sxth w8, w0
39+
; CHECK-NEXT: cmp w8, #0
40+
; CHECK-NEXT: csel w8, w1, w2, ge
41+
; CHECK-NEXT: add w0, w8, w0, uxth
4642
; CHECK-NEXT: ret
4743
entry:
4844
%cmp = icmp sgt i16 %in, -1
@@ -61,11 +57,9 @@ B:
6157
define i64 @f_i32_sign_extend_inreg(i32 %in, i64 %a, i64 %b) nounwind {
6258
; CHECK-LABEL: f_i32_sign_extend_inreg:
6359
; CHECK: // %bb.0: // %entry
64-
; CHECK-NEXT: mov w8, w0
6560
; CHECK-NEXT: cmp w0, #0
66-
; CHECK-NEXT: add x9, x8, x1
67-
; CHECK-NEXT: add x8, x8, x2
68-
; CHECK-NEXT: csel x0, x9, x8, ge
61+
; CHECK-NEXT: csel x8, x1, x2, ge
62+
; CHECK-NEXT: add x0, x8, w0, uxtw
6963
; CHECK-NEXT: ret
7064
entry:
7165
%cmp = icmp sgt i32 %in, -1
@@ -84,12 +78,10 @@ B:
8478
define i32 @g_i8_sign_extend_inreg(i8 %in, i32 %a, i32 %b) nounwind {
8579
; CHECK-LABEL: g_i8_sign_extend_inreg:
8680
; CHECK: // %bb.0: // %entry
87-
; CHECK-NEXT: and w8, w0, #0xff
88-
; CHECK-NEXT: sxtb w9, w0
89-
; CHECK-NEXT: add w10, w8, w1
90-
; CHECK-NEXT: add w8, w8, w2
91-
; CHECK-NEXT: cmp w9, #0
92-
; CHECK-NEXT: csel w0, w10, w8, lt
81+
; CHECK-NEXT: sxtb w8, w0
82+
; CHECK-NEXT: cmp w8, #0
83+
; CHECK-NEXT: csel w8, w1, w2, lt
84+
; CHECK-NEXT: add w0, w8, w0, uxtb
9385
; CHECK-NEXT: ret
9486
entry:
9587
%cmp = icmp slt i8 %in, 0
@@ -108,12 +100,10 @@ B:
108100
define i32 @g_i16_sign_extend_inreg(i16 %in, i32 %a, i32 %b) nounwind {
109101
; CHECK-LABEL: g_i16_sign_extend_inreg:
110102
; CHECK: // %bb.0: // %entry
111-
; CHECK-NEXT: and w8, w0, #0xffff
112-
; CHECK-NEXT: sxth w9, w0
113-
; CHECK-NEXT: add w10, w8, w1
114-
; CHECK-NEXT: add w8, w8, w2
115-
; CHECK-NEXT: cmp w9, #0
116-
; CHECK-NEXT: csel w0, w10, w8, lt
103+
; CHECK-NEXT: sxth w8, w0
104+
; CHECK-NEXT: cmp w8, #0
105+
; CHECK-NEXT: csel w8, w1, w2, lt
106+
; CHECK-NEXT: add w0, w8, w0, uxth
117107
; CHECK-NEXT: ret
118108
entry:
119109
%cmp = icmp slt i16 %in, 0
@@ -132,11 +122,9 @@ B:
132122
define i64 @g_i32_sign_extend_inreg(i32 %in, i64 %a, i64 %b) nounwind {
133123
; CHECK-LABEL: g_i32_sign_extend_inreg:
134124
; CHECK: // %bb.0: // %entry
135-
; CHECK-NEXT: mov w8, w0
136125
; CHECK-NEXT: cmp w0, #0
137-
; CHECK-NEXT: add x9, x8, x1
138-
; CHECK-NEXT: add x8, x8, x2
139-
; CHECK-NEXT: csel x0, x9, x8, lt
126+
; CHECK-NEXT: csel x8, x1, x2, lt
127+
; CHECK-NEXT: add x0, x8, w0, uxtw
140128
; CHECK-NEXT: ret
141129
entry:
142130
%cmp = icmp slt i32 %in, 0
@@ -156,12 +144,10 @@ define i64 @f_i32_sign_extend_i64(i32 %in, i64 %a, i64 %b) nounwind {
156144
; CHECK-LABEL: f_i32_sign_extend_i64:
157145
; CHECK: // %bb.0: // %entry
158146
; CHECK-NEXT: // kill: def $w0 killed $w0 def $x0
159-
; CHECK-NEXT: mov w8, w0
160-
; CHECK-NEXT: sxtw x9, w0
161-
; CHECK-NEXT: add x10, x8, x1
162-
; CHECK-NEXT: add x8, x8, x2
163-
; CHECK-NEXT: cmp x9, #0
164-
; CHECK-NEXT: csel x0, x10, x8, ge
147+
; CHECK-NEXT: sxtw x8, w0
148+
; CHECK-NEXT: cmp x8, #0
149+
; CHECK-NEXT: csel x8, x1, x2, ge
150+
; CHECK-NEXT: add x0, x8, w0, uxtw
165151
; CHECK-NEXT: ret
166152
entry:
167153
%inext = sext i32 %in to i64
@@ -182,12 +168,10 @@ define i64 @g_i32_sign_extend_i64(i32 %in, i64 %a, i64 %b) nounwind {
182168
; CHECK-LABEL: g_i32_sign_extend_i64:
183169
; CHECK: // %bb.0: // %entry
184170
; CHECK-NEXT: // kill: def $w0 killed $w0 def $x0
185-
; CHECK-NEXT: mov w8, w0
186-
; CHECK-NEXT: sxtw x9, w0
187-
; CHECK-NEXT: add x10, x8, x1
188-
; CHECK-NEXT: add x8, x8, x2
189-
; CHECK-NEXT: cmp x9, #0
190-
; CHECK-NEXT: csel x0, x10, x8, lt
171+
; CHECK-NEXT: sxtw x8, w0
172+
; CHECK-NEXT: cmp x8, #0
173+
; CHECK-NEXT: csel x8, x1, x2, lt
174+
; CHECK-NEXT: add x0, x8, w0, uxtw
191175
; CHECK-NEXT: ret
192176
entry:
193177
%inext = sext i32 %in to i64

llvm/test/CodeGen/AArch64/combine-comparisons-by-cse.ll

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ define i32 @fcmpri(i32 %argc, i8** nocapture readonly %argv) {
654654
; CHECK-NEXT: cbz x8, .LBB9_3
655655
; CHECK-NEXT: // %bb.2:
656656
; CHECK-NEXT: mov w0, #3
657-
; CHECK-NEXT: b .LBB9_6
657+
; CHECK-NEXT: b .LBB9_4
658658
; CHECK-NEXT: .LBB9_3: // %if.end
659659
; CHECK-NEXT: mov w0, #1
660660
; CHECK-NEXT: bl zoo
@@ -666,17 +666,14 @@ define i32 @fcmpri(i32 %argc, i8** nocapture readonly %argv) {
666666
; CHECK-NEXT: cinc w0, w19, gt
667667
; CHECK-NEXT: fmov d8, d0
668668
; CHECK-NEXT: bl xoo
669-
; CHECK-NEXT: fcmp d8, #0.0
670-
; CHECK-NEXT: b.gt .LBB9_5
671-
; CHECK-NEXT: // %bb.4: // %cond.false12
672669
; CHECK-NEXT: fmov d0, #-1.00000000
673-
; CHECK-NEXT: fadd d8, d8, d0
674-
; CHECK-NEXT: .LBB9_5: // %cond.end14
675-
; CHECK-NEXT: fmov d0, d8
670+
; CHECK-NEXT: fcmp d8, #0.0
676671
; CHECK-NEXT: fmov d1, #-2.00000000
672+
; CHECK-NEXT: fadd d0, d8, d0
673+
; CHECK-NEXT: fcsel d0, d8, d0, gt
677674
; CHECK-NEXT: bl woo
678675
; CHECK-NEXT: mov w0, #4
679-
; CHECK-NEXT: .LBB9_6: // %return
676+
; CHECK-NEXT: .LBB9_4: // %return
680677
; CHECK-NEXT: ldp x30, x19, [sp, #16] // 16-byte Folded Reload
681678
; CHECK-NEXT: ldr d8, [sp], #32 // 8-byte Folded Reload
682679
; CHECK-NEXT: ret

0 commit comments

Comments
 (0)