Skip to content

Commit 431969e

Browse files
committed
Revert "[SimplifyCFG] Transform for redirecting phis between unmergeable BB and SuccBB (#67275)"
This reverts commit fc86d03. This change breaks LLVM buildbot clang-aarch64-sve-vls-2stage https://lab.llvm.org/buildbot/#/builders/176/builds/5474 I am going to revert this patch as the bot has been failing for more than a day without a fix.
1 parent 5c6eefb commit 431969e

File tree

7 files changed

+70
-789
lines changed

7 files changed

+70
-789
lines changed

llvm/lib/Transforms/Utils/Local.cpp

+38-128
Original file line numberDiff line numberDiff line change
@@ -847,17 +847,17 @@ static bool CanMergeValues(Value *First, Value *Second) {
847847
/// branch to Succ, into Succ.
848848
///
849849
/// Assumption: Succ is the single successor for BB.
850-
static bool
851-
CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ,
852-
const SmallPtrSetImpl<BasicBlock *> &BBPreds) {
850+
static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
853851
assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
854852

855853
LLVM_DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
856854
<< Succ->getName() << "\n");
857855
// Shortcut, if there is only a single predecessor it must be BB and merging
858856
// is always safe
859-
if (Succ->getSinglePredecessor())
860-
return true;
857+
if (Succ->getSinglePredecessor()) return true;
858+
859+
// Make a list of the predecessors of BB
860+
SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
861861

862862
// Look at all the phi nodes in Succ, to see if they present a conflict when
863863
// merging these blocks
@@ -997,47 +997,16 @@ static void replaceUndefValuesInPhi(PHINode *PN,
997997
}
998998
}
999999

1000-
// Only when they shares a single common predecessor, return true.
1001-
// Only handles cases when BB can't be merged while its predecessors can be
1002-
// redirected.
1003-
static bool
1004-
CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
1005-
const SmallPtrSetImpl<BasicBlock *> &BBPreds,
1006-
const SmallPtrSetImpl<BasicBlock *> &SuccPreds,
1007-
BasicBlock *&CommonPred) {
1008-
1009-
// There must be phis in BB, otherwise BB will be merged into Succ directly
1010-
if (BB->phis().empty() || Succ->phis().empty())
1011-
return false;
1012-
1013-
// BB must have predecessors not shared that can be redirected to Succ
1014-
if (!BB->hasNPredecessorsOrMore(2))
1015-
return false;
1016-
1017-
// Get single common predecessors of both BB and Succ
1018-
for (BasicBlock *SuccPred : SuccPreds) {
1019-
if (BBPreds.count(SuccPred)) {
1020-
if (CommonPred)
1021-
return false;
1022-
CommonPred = SuccPred;
1023-
}
1024-
}
1025-
1026-
return true;
1027-
}
1028-
10291000
/// Replace a value flowing from a block to a phi with
10301001
/// potentially multiple instances of that value flowing from the
10311002
/// block's predecessors to the phi.
10321003
///
10331004
/// \param BB The block with the value flowing into the phi.
10341005
/// \param BBPreds The predecessors of BB.
10351006
/// \param PN The phi that we are updating.
1036-
/// \param CommonPred The common predecessor of BB and PN's BasicBlock
10371007
static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
10381008
const PredBlockVector &BBPreds,
1039-
PHINode *PN,
1040-
BasicBlock *CommonPred) {
1009+
PHINode *PN) {
10411010
Value *OldVal = PN->removeIncomingValue(BB, false);
10421011
assert(OldVal && "No entry in PHI for Pred BB!");
10431012

@@ -1065,39 +1034,26 @@ static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
10651034
// will trigger asserts if we try to clean it up now, without also
10661035
// simplifying the corresponding conditional branch).
10671036
BasicBlock *PredBB = OldValPN->getIncomingBlock(i);
1068-
1069-
if (PredBB == CommonPred)
1070-
continue;
1071-
10721037
Value *PredVal = OldValPN->getIncomingValue(i);
1073-
Value *Selected =
1074-
selectIncomingValueForBlock(PredVal, PredBB, IncomingValues);
1038+
Value *Selected = selectIncomingValueForBlock(PredVal, PredBB,
1039+
IncomingValues);
10751040

10761041
// And add a new incoming value for this predecessor for the
10771042
// newly retargeted branch.
10781043
PN->addIncoming(Selected, PredBB);
10791044
}
1080-
if (CommonPred)
1081-
PN->addIncoming(OldValPN->getIncomingValueForBlock(CommonPred), BB);
1082-
10831045
} else {
10841046
for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) {
10851047
// Update existing incoming values in PN for this
10861048
// predecessor of BB.
10871049
BasicBlock *PredBB = BBPreds[i];
1088-
1089-
if (PredBB == CommonPred)
1090-
continue;
1091-
1092-
Value *Selected =
1093-
selectIncomingValueForBlock(OldVal, PredBB, IncomingValues);
1050+
Value *Selected = selectIncomingValueForBlock(OldVal, PredBB,
1051+
IncomingValues);
10941052

10951053
// And add a new incoming value for this predecessor for the
10961054
// newly retargeted branch.
10971055
PN->addIncoming(Selected, PredBB);
10981056
}
1099-
if (CommonPred)
1100-
PN->addIncoming(OldVal, BB);
11011057
}
11021058

11031059
replaceUndefValuesInPhi(PN, IncomingValues);
@@ -1108,30 +1064,13 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11081064
assert(BB != &BB->getParent()->getEntryBlock() &&
11091065
"TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
11101066

1111-
// We can't simplify infinite loops.
1067+
// We can't eliminate infinite loops.
11121068
BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
1113-
if (BB == Succ)
1114-
return false;
1115-
1116-
SmallPtrSet<BasicBlock *, 16> BBPreds(pred_begin(BB), pred_end(BB));
1117-
SmallPtrSet<BasicBlock *, 16> SuccPreds(pred_begin(Succ), pred_end(Succ));
1069+
if (BB == Succ) return false;
11181070

1119-
// The single common predecessor of BB and Succ when BB cannot be killed
1120-
BasicBlock *CommonPred = nullptr;
1121-
1122-
bool BBKillable = CanPropagatePredecessorsForPHIs(BB, Succ, BBPreds);
1123-
1124-
// Even if we can not fold bB into Succ, we may be able to redirect the
1125-
// predecessors of BB to Succ.
1126-
bool BBPhisMergeable =
1127-
BBKillable ||
1128-
CanRedirectPredsOfEmptyBBToSucc(BB, Succ, BBPreds, SuccPreds, CommonPred);
1129-
1130-
if (!BBKillable && !BBPhisMergeable)
1131-
return false;
1132-
1133-
// Check to see if merging these blocks/phis would cause conflicts for any of
1134-
// the phi nodes in BB or Succ. If not, we can safely merge.
1071+
// Check to see if merging these blocks would cause conflicts for any of the
1072+
// phi nodes in BB or Succ. If not, we can safely merge.
1073+
if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
11351074

11361075
// Check for cases where Succ has multiple predecessors and a PHI node in BB
11371076
// has uses which will not disappear when the PHI nodes are merged. It is
@@ -1160,11 +1099,6 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11601099
}
11611100
}
11621101

1163-
if (BBPhisMergeable && CommonPred)
1164-
LLVM_DEBUG(dbgs() << "Found Common Predecessor between: " << BB->getName()
1165-
<< " and " << Succ->getName() << " : "
1166-
<< CommonPred->getName() << "\n");
1167-
11681102
// 'BB' and 'BB->Pred' are loop latches, bail out to presrve inner loop
11691103
// metadata.
11701104
//
@@ -1237,37 +1171,25 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
12371171
if (PredTI->hasMetadata(LLVMContext::MD_loop))
12381172
return false;
12391173

1240-
if (BBKillable)
1241-
LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
1242-
else if (BBPhisMergeable)
1243-
LLVM_DEBUG(dbgs() << "Merge Phis in Trivial BB: \n" << *BB);
1174+
LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
12441175

12451176
SmallVector<DominatorTree::UpdateType, 32> Updates;
1246-
12471177
if (DTU) {
12481178
// To avoid processing the same predecessor more than once.
12491179
SmallPtrSet<BasicBlock *, 8> SeenPreds;
1250-
// All predecessors of BB (except the common predecessor) will be moved to
1251-
// Succ.
1180+
// All predecessors of BB will be moved to Succ.
1181+
SmallPtrSet<BasicBlock *, 8> PredsOfSucc(pred_begin(Succ), pred_end(Succ));
12521182
Updates.reserve(Updates.size() + 2 * pred_size(BB) + 1);
1253-
1254-
for (auto *PredOfBB : predecessors(BB)) {
1255-
// Do not modify those common predecessors of BB and Succ
1256-
if (!SuccPreds.contains(PredOfBB))
1183+
for (auto *PredOfBB : predecessors(BB))
1184+
// This predecessor of BB may already have Succ as a successor.
1185+
if (!PredsOfSucc.contains(PredOfBB))
12571186
if (SeenPreds.insert(PredOfBB).second)
12581187
Updates.push_back({DominatorTree::Insert, PredOfBB, Succ});
1259-
}
1260-
12611188
SeenPreds.clear();
1262-
12631189
for (auto *PredOfBB : predecessors(BB))
1264-
// When BB cannot be killed, do not remove the edge between BB and
1265-
// CommonPred.
1266-
if (SeenPreds.insert(PredOfBB).second && PredOfBB != CommonPred)
1190+
if (SeenPreds.insert(PredOfBB).second)
12671191
Updates.push_back({DominatorTree::Delete, PredOfBB, BB});
1268-
1269-
if (BBKillable)
1270-
Updates.push_back({DominatorTree::Delete, BB, Succ});
1192+
Updates.push_back({DominatorTree::Delete, BB, Succ});
12711193
}
12721194

12731195
if (isa<PHINode>(Succ->begin())) {
@@ -1279,19 +1201,21 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
12791201
// Loop over all of the PHI nodes in the successor of BB.
12801202
for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
12811203
PHINode *PN = cast<PHINode>(I);
1282-
redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN, CommonPred);
1204+
1205+
redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN);
12831206
}
12841207
}
12851208

12861209
if (Succ->getSinglePredecessor()) {
12871210
// BB is the only predecessor of Succ, so Succ will end up with exactly
12881211
// the same predecessors BB had.
1212+
12891213
// Copy over any phi, debug or lifetime instruction.
12901214
BB->getTerminator()->eraseFromParent();
12911215
Succ->splice(Succ->getFirstNonPHI()->getIterator(), BB);
12921216
} else {
12931217
while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
1294-
// We explicitly check for such uses for merging phis.
1218+
// We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
12951219
assert(PN->use_empty() && "There shouldn't be any uses here!");
12961220
PN->eraseFromParent();
12971221
}
@@ -1304,35 +1228,21 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
13041228
for (BasicBlock *Pred : predecessors(BB))
13051229
Pred->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopMD);
13061230

1307-
if (BBKillable) {
1308-
// Everything that jumped to BB now goes to Succ.
1309-
BB->replaceAllUsesWith(Succ);
1310-
1311-
if (!Succ->hasName())
1312-
Succ->takeName(BB);
1313-
1314-
// Clear the successor list of BB to match updates applying to DTU later.
1315-
if (BB->getTerminator())
1316-
BB->back().eraseFromParent();
1317-
1318-
new UnreachableInst(BB->getContext(), BB);
1319-
assert(succ_empty(BB) && "The successor list of BB isn't empty before "
1320-
"applying corresponding DTU updates.");
1321-
} else if (BBPhisMergeable) {
1322-
// Everything except CommonPred that jumped to BB now goes to Succ.
1323-
BB->replaceUsesWithIf(Succ, [BBPreds, CommonPred](Use &U) -> bool {
1324-
if (Instruction *UseInst = dyn_cast<Instruction>(U.getUser()))
1325-
return UseInst->getParent() != CommonPred &&
1326-
BBPreds.contains(UseInst->getParent());
1327-
return false;
1328-
});
1329-
}
1231+
// Everything that jumped to BB now goes to Succ.
1232+
BB->replaceAllUsesWith(Succ);
1233+
if (!Succ->hasName()) Succ->takeName(BB);
1234+
1235+
// Clear the successor list of BB to match updates applying to DTU later.
1236+
if (BB->getTerminator())
1237+
BB->back().eraseFromParent();
1238+
new UnreachableInst(BB->getContext(), BB);
1239+
assert(succ_empty(BB) && "The successor list of BB isn't empty before "
1240+
"applying corresponding DTU updates.");
13301241

13311242
if (DTU)
13321243
DTU->applyUpdates(Updates);
13331244

1334-
if (BBKillable)
1335-
DeleteDeadBlock(BB, DTU);
1245+
DeleteDeadBlock(BB, DTU);
13361246

13371247
return true;
13381248
}

llvm/test/CodeGen/ARM/jump-table-islands.ll

-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
%BigInt = type i8500
44

5-
declare void @use(%BigInt)
6-
75
define %BigInt @test_moved_jumptable(i1 %tst, i32 %sw, %BigInt %l) {
86
; CHECK-LABEL: test_moved_jumptable:
97

@@ -36,8 +34,6 @@ other:
3634

3735
end:
3836
%val = phi %BigInt [ %l, %complex ], [ -1, %simple ]
39-
; Prevent SimplifyCFG from simplifying the phi node above.
40-
call void @use(%BigInt %val)
4137
ret %BigInt %val
4238
}
4339

llvm/test/Transforms/JumpThreading/codesize-loop.ll

+27-21
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ define i32 @test_minsize(i32 %argc, ptr nocapture readonly %argv) local_unnamed_
4242
; OVERIDE-NEXT: [[TMP3:%.*]] = mul i32 [[CALL]], [[TMP2]]
4343
; OVERIDE-NEXT: [[TMP4:%.*]] = icmp sgt i32 [[CALL]], 0
4444
; OVERIDE-NEXT: [[COND_FR:%.*]] = freeze i1 [[TMP4]]
45-
; OVERIDE-NEXT: br i1 [[COND_FR]], label [[TMP5:%.*]], label [[COND_END_THREAD]]
46-
; OVERIDE: 5:
47-
; OVERIDE-NEXT: br label [[COND_END_THREAD]]
45+
; OVERIDE-NEXT: br i1 [[COND_FR]], label [[COND_END_THREAD]], label [[TMP6:%.*]]
4846
; OVERIDE: cond.end.thread:
49-
; OVERIDE-NEXT: [[TMP6:%.*]] = phi i32 [ [[TMP3]], [[COND_END]] ], [ [[TMP3]], [[TMP5]] ], [ 205962976, [[ENTRY:%.*]] ]
50-
; OVERIDE-NEXT: [[TMP7:%.*]] = phi i32 [ 0, [[COND_END]] ], [ [[CALL]], [[TMP5]] ], [ 46, [[ENTRY]] ]
51-
; OVERIDE-NEXT: [[TMP8:%.*]] = mul i32 [[TMP6]], [[TMP7]]
52-
; OVERIDE-NEXT: [[CALL33:%.*]] = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @.str, i32 [[TMP8]])
47+
; OVERIDE-NEXT: [[TMP5:%.*]] = phi i32 [ [[TMP3]], [[COND_END]] ], [ 205962976, [[ENTRY:%.*]] ]
48+
; OVERIDE-NEXT: [[COND3:%.*]] = phi i32 [ [[CALL]], [[COND_END]] ], [ 46, [[ENTRY]] ]
49+
; OVERIDE-NEXT: br label [[TMP6]]
50+
; OVERIDE: 6:
51+
; OVERIDE-NEXT: [[TMP7:%.*]] = phi i32 [ [[TMP5]], [[COND_END_THREAD]] ], [ [[TMP3]], [[COND_END]] ]
52+
; OVERIDE-NEXT: [[TMP8:%.*]] = phi i32 [ [[COND3]], [[COND_END_THREAD]] ], [ 0, [[COND_END]] ]
53+
; OVERIDE-NEXT: [[TMP9:%.*]] = mul i32 [[TMP7]], [[TMP8]]
54+
; OVERIDE-NEXT: [[CALL33:%.*]] = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @.str, i32 [[TMP9]])
5355
; OVERIDE-NEXT: ret i32 0
5456
;
5557
entry:
@@ -88,14 +90,16 @@ define i32 @test_optsize(i32 %argc, ptr nocapture readonly %argv) local_unnamed_
8890
; DEFAULT-NEXT: [[TMP3:%.*]] = mul i32 [[CALL]], [[TMP2]]
8991
; DEFAULT-NEXT: [[TMP4:%.*]] = icmp sgt i32 [[CALL]], 0
9092
; DEFAULT-NEXT: [[COND_FR:%.*]] = freeze i1 [[TMP4]]
91-
; DEFAULT-NEXT: br i1 [[COND_FR]], label [[TMP5:%.*]], label [[COND_END_THREAD]]
92-
; DEFAULT: 5:
93-
; DEFAULT-NEXT: br label [[COND_END_THREAD]]
93+
; DEFAULT-NEXT: br i1 [[COND_FR]], label [[COND_END_THREAD]], label [[TMP6:%.*]]
9494
; DEFAULT: cond.end.thread:
95-
; DEFAULT-NEXT: [[TMP6:%.*]] = phi i32 [ [[TMP3]], [[COND_END]] ], [ [[TMP3]], [[TMP5]] ], [ 205962976, [[ENTRY:%.*]] ]
96-
; DEFAULT-NEXT: [[TMP7:%.*]] = phi i32 [ 0, [[COND_END]] ], [ [[CALL]], [[TMP5]] ], [ 46, [[ENTRY]] ]
97-
; DEFAULT-NEXT: [[TMP8:%.*]] = mul i32 [[TMP6]], [[TMP7]]
98-
; DEFAULT-NEXT: [[CALL33:%.*]] = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @.str, i32 [[TMP8]])
95+
; DEFAULT-NEXT: [[TMP5:%.*]] = phi i32 [ [[TMP3]], [[COND_END]] ], [ 205962976, [[ENTRY:%.*]] ]
96+
; DEFAULT-NEXT: [[COND3:%.*]] = phi i32 [ [[CALL]], [[COND_END]] ], [ 46, [[ENTRY]] ]
97+
; DEFAULT-NEXT: br label [[TMP6]]
98+
; DEFAULT: 6:
99+
; DEFAULT-NEXT: [[TMP7:%.*]] = phi i32 [ [[TMP5]], [[COND_END_THREAD]] ], [ [[TMP3]], [[COND_END]] ]
100+
; DEFAULT-NEXT: [[TMP8:%.*]] = phi i32 [ [[COND3]], [[COND_END_THREAD]] ], [ 0, [[COND_END]] ]
101+
; DEFAULT-NEXT: [[TMP9:%.*]] = mul i32 [[TMP7]], [[TMP8]]
102+
; DEFAULT-NEXT: [[CALL33:%.*]] = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @.str, i32 [[TMP9]])
99103
; DEFAULT-NEXT: ret i32 0
100104
;
101105
; OVERIDE-LABEL: @test_optsize(
@@ -111,14 +115,16 @@ define i32 @test_optsize(i32 %argc, ptr nocapture readonly %argv) local_unnamed_
111115
; OVERIDE-NEXT: [[TMP3:%.*]] = mul i32 [[CALL]], [[TMP2]]
112116
; OVERIDE-NEXT: [[TMP4:%.*]] = icmp sgt i32 [[CALL]], 0
113117
; OVERIDE-NEXT: [[COND_FR:%.*]] = freeze i1 [[TMP4]]
114-
; OVERIDE-NEXT: br i1 [[COND_FR]], label [[TMP5:%.*]], label [[COND_END_THREAD]]
115-
; OVERIDE: 5:
116-
; OVERIDE-NEXT: br label [[COND_END_THREAD]]
118+
; OVERIDE-NEXT: br i1 [[COND_FR]], label [[COND_END_THREAD]], label [[TMP6:%.*]]
117119
; OVERIDE: cond.end.thread:
118-
; OVERIDE-NEXT: [[TMP6:%.*]] = phi i32 [ [[TMP3]], [[COND_END]] ], [ [[TMP3]], [[TMP5]] ], [ 205962976, [[ENTRY:%.*]] ]
119-
; OVERIDE-NEXT: [[TMP7:%.*]] = phi i32 [ 0, [[COND_END]] ], [ [[CALL]], [[TMP5]] ], [ 46, [[ENTRY]] ]
120-
; OVERIDE-NEXT: [[TMP8:%.*]] = mul i32 [[TMP6]], [[TMP7]]
121-
; OVERIDE-NEXT: [[CALL33:%.*]] = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @.str, i32 [[TMP8]])
120+
; OVERIDE-NEXT: [[TMP5:%.*]] = phi i32 [ [[TMP3]], [[COND_END]] ], [ 205962976, [[ENTRY:%.*]] ]
121+
; OVERIDE-NEXT: [[COND3:%.*]] = phi i32 [ [[CALL]], [[COND_END]] ], [ 46, [[ENTRY]] ]
122+
; OVERIDE-NEXT: br label [[TMP6]]
123+
; OVERIDE: 6:
124+
; OVERIDE-NEXT: [[TMP7:%.*]] = phi i32 [ [[TMP5]], [[COND_END_THREAD]] ], [ [[TMP3]], [[COND_END]] ]
125+
; OVERIDE-NEXT: [[TMP8:%.*]] = phi i32 [ [[COND3]], [[COND_END_THREAD]] ], [ 0, [[COND_END]] ]
126+
; OVERIDE-NEXT: [[TMP9:%.*]] = mul i32 [[TMP7]], [[TMP8]]
127+
; OVERIDE-NEXT: [[CALL33:%.*]] = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @.str, i32 [[TMP9]])
122128
; OVERIDE-NEXT: ret i32 0
123129
;
124130
entry:

0 commit comments

Comments
 (0)