Skip to content

Commit fc6bdb8

Browse files
authored
[SimplifyCFG] Reland transform for redirecting phis between unmergeable BB and SuccBB (#68473)
Reland #67275 with #68953 resolved.
1 parent 8db38bd commit fc6bdb8

File tree

7 files changed

+789
-70
lines changed

7 files changed

+789
-70
lines changed

llvm/lib/Transforms/Utils/Local.cpp

+128-38
Original file line numberDiff line numberDiff line change
@@ -850,17 +850,17 @@ static bool CanMergeValues(Value *First, Value *Second) {
850850
/// branch to Succ, into Succ.
851851
///
852852
/// Assumption: Succ is the single successor for BB.
853-
static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
853+
static bool
854+
CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ,
855+
const SmallPtrSetImpl<BasicBlock *> &BBPreds) {
854856
assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
855857

856858
LLVM_DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
857859
<< Succ->getName() << "\n");
858860
// Shortcut, if there is only a single predecessor it must be BB and merging
859861
// is always safe
860-
if (Succ->getSinglePredecessor()) return true;
861-
862-
// Make a list of the predecessors of BB
863-
SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
862+
if (Succ->getSinglePredecessor())
863+
return true;
864864

865865
// Look at all the phi nodes in Succ, to see if they present a conflict when
866866
// merging these blocks
@@ -1000,16 +1000,47 @@ static void replaceUndefValuesInPhi(PHINode *PN,
10001000
}
10011001
}
10021002

1003+
// Only when they shares a single common predecessor, return true.
1004+
// Only handles cases when BB can't be merged while its predecessors can be
1005+
// redirected.
1006+
static bool
1007+
CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
1008+
const SmallPtrSetImpl<BasicBlock *> &BBPreds,
1009+
const SmallPtrSetImpl<BasicBlock *> &SuccPreds,
1010+
BasicBlock *&CommonPred) {
1011+
1012+
// There must be phis in BB, otherwise BB will be merged into Succ directly
1013+
if (BB->phis().empty() || Succ->phis().empty())
1014+
return false;
1015+
1016+
// BB must have predecessors not shared that can be redirected to Succ
1017+
if (!BB->hasNPredecessorsOrMore(2))
1018+
return false;
1019+
1020+
// Get single common predecessors of both BB and Succ
1021+
for (BasicBlock *SuccPred : SuccPreds) {
1022+
if (BBPreds.count(SuccPred)) {
1023+
if (CommonPred)
1024+
return false;
1025+
CommonPred = SuccPred;
1026+
}
1027+
}
1028+
1029+
return true;
1030+
}
1031+
10031032
/// Replace a value flowing from a block to a phi with
10041033
/// potentially multiple instances of that value flowing from the
10051034
/// block's predecessors to the phi.
10061035
///
10071036
/// \param BB The block with the value flowing into the phi.
10081037
/// \param BBPreds The predecessors of BB.
10091038
/// \param PN The phi that we are updating.
1039+
/// \param CommonPred The common predecessor of BB and PN's BasicBlock
10101040
static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
10111041
const PredBlockVector &BBPreds,
1012-
PHINode *PN) {
1042+
PHINode *PN,
1043+
BasicBlock *CommonPred) {
10131044
Value *OldVal = PN->removeIncomingValue(BB, false);
10141045
assert(OldVal && "No entry in PHI for Pred BB!");
10151046

@@ -1037,26 +1068,39 @@ static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
10371068
// will trigger asserts if we try to clean it up now, without also
10381069
// simplifying the corresponding conditional branch).
10391070
BasicBlock *PredBB = OldValPN->getIncomingBlock(i);
1071+
1072+
if (PredBB == CommonPred)
1073+
continue;
1074+
10401075
Value *PredVal = OldValPN->getIncomingValue(i);
1041-
Value *Selected = selectIncomingValueForBlock(PredVal, PredBB,
1042-
IncomingValues);
1076+
Value *Selected =
1077+
selectIncomingValueForBlock(PredVal, PredBB, IncomingValues);
10431078

10441079
// And add a new incoming value for this predecessor for the
10451080
// newly retargeted branch.
10461081
PN->addIncoming(Selected, PredBB);
10471082
}
1083+
if (CommonPred)
1084+
PN->addIncoming(OldValPN->getIncomingValueForBlock(CommonPred), BB);
1085+
10481086
} else {
10491087
for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) {
10501088
// Update existing incoming values in PN for this
10511089
// predecessor of BB.
10521090
BasicBlock *PredBB = BBPreds[i];
1053-
Value *Selected = selectIncomingValueForBlock(OldVal, PredBB,
1054-
IncomingValues);
1091+
1092+
if (PredBB == CommonPred)
1093+
continue;
1094+
1095+
Value *Selected =
1096+
selectIncomingValueForBlock(OldVal, PredBB, IncomingValues);
10551097

10561098
// And add a new incoming value for this predecessor for the
10571099
// newly retargeted branch.
10581100
PN->addIncoming(Selected, PredBB);
10591101
}
1102+
if (CommonPred)
1103+
PN->addIncoming(OldVal, BB);
10601104
}
10611105

10621106
replaceUndefValuesInPhi(PN, IncomingValues);
@@ -1067,13 +1111,30 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
10671111
assert(BB != &BB->getParent()->getEntryBlock() &&
10681112
"TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
10691113

1070-
// We can't eliminate infinite loops.
1114+
// We can't simplify infinite loops.
10711115
BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
1072-
if (BB == Succ) return false;
1116+
if (BB == Succ)
1117+
return false;
1118+
1119+
SmallPtrSet<BasicBlock *, 16> BBPreds(pred_begin(BB), pred_end(BB));
1120+
SmallPtrSet<BasicBlock *, 16> SuccPreds(pred_begin(Succ), pred_end(Succ));
10731121

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

10781139
// Check for cases where Succ has multiple predecessors and a PHI node in BB
10791140
// has uses which will not disappear when the PHI nodes are merged. It is
@@ -1102,6 +1163,11 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11021163
}
11031164
}
11041165

1166+
if (BBPhisMergeable && CommonPred)
1167+
LLVM_DEBUG(dbgs() << "Found Common Predecessor between: " << BB->getName()
1168+
<< " and " << Succ->getName() << " : "
1169+
<< CommonPred->getName() << "\n");
1170+
11051171
// 'BB' and 'BB->Pred' are loop latches, bail out to presrve inner loop
11061172
// metadata.
11071173
//
@@ -1174,25 +1240,37 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11741240
if (PredTI->hasMetadata(LLVMContext::MD_loop))
11751241
return false;
11761242

1177-
LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
1243+
if (BBKillable)
1244+
LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
1245+
else if (BBPhisMergeable)
1246+
LLVM_DEBUG(dbgs() << "Merge Phis in Trivial BB: \n" << *BB);
11781247

11791248
SmallVector<DominatorTree::UpdateType, 32> Updates;
1249+
11801250
if (DTU) {
11811251
// To avoid processing the same predecessor more than once.
11821252
SmallPtrSet<BasicBlock *, 8> SeenPreds;
1183-
// All predecessors of BB will be moved to Succ.
1184-
SmallPtrSet<BasicBlock *, 8> PredsOfSucc(pred_begin(Succ), pred_end(Succ));
1253+
// All predecessors of BB (except the common predecessor) will be moved to
1254+
// Succ.
11851255
Updates.reserve(Updates.size() + 2 * pred_size(BB) + 1);
1186-
for (auto *PredOfBB : predecessors(BB))
1187-
// This predecessor of BB may already have Succ as a successor.
1188-
if (!PredsOfSucc.contains(PredOfBB))
1256+
1257+
for (auto *PredOfBB : predecessors(BB)) {
1258+
// Do not modify those common predecessors of BB and Succ
1259+
if (!SuccPreds.contains(PredOfBB))
11891260
if (SeenPreds.insert(PredOfBB).second)
11901261
Updates.push_back({DominatorTree::Insert, PredOfBB, Succ});
1262+
}
1263+
11911264
SeenPreds.clear();
1265+
11921266
for (auto *PredOfBB : predecessors(BB))
1193-
if (SeenPreds.insert(PredOfBB).second)
1267+
// When BB cannot be killed, do not remove the edge between BB and
1268+
// CommonPred.
1269+
if (SeenPreds.insert(PredOfBB).second && PredOfBB != CommonPred)
11941270
Updates.push_back({DominatorTree::Delete, PredOfBB, BB});
1195-
Updates.push_back({DominatorTree::Delete, BB, Succ});
1271+
1272+
if (BBKillable)
1273+
Updates.push_back({DominatorTree::Delete, BB, Succ});
11961274
}
11971275

11981276
if (isa<PHINode>(Succ->begin())) {
@@ -1204,21 +1282,19 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
12041282
// Loop over all of the PHI nodes in the successor of BB.
12051283
for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
12061284
PHINode *PN = cast<PHINode>(I);
1207-
1208-
redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN);
1285+
redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN, CommonPred);
12091286
}
12101287
}
12111288

12121289
if (Succ->getSinglePredecessor()) {
12131290
// BB is the only predecessor of Succ, so Succ will end up with exactly
12141291
// the same predecessors BB had.
1215-
12161292
// Copy over any phi, debug or lifetime instruction.
12171293
BB->getTerminator()->eraseFromParent();
12181294
Succ->splice(Succ->getFirstNonPHI()->getIterator(), BB);
12191295
} else {
12201296
while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
1221-
// We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
1297+
// We explicitly check for such uses for merging phis.
12221298
assert(PN->use_empty() && "There shouldn't be any uses here!");
12231299
PN->eraseFromParent();
12241300
}
@@ -1231,21 +1307,35 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
12311307
for (BasicBlock *Pred : predecessors(BB))
12321308
Pred->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopMD);
12331309

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

12451334
if (DTU)
12461335
DTU->applyUpdates(Updates);
12471336

1248-
DeleteDeadBlock(BB, DTU);
1337+
if (BBKillable)
1338+
DeleteDeadBlock(BB, DTU);
12491339

12501340
return true;
12511341
}

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

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

33
%BigInt = type i8500
44

5+
declare void @use(%BigInt)
6+
57
define %BigInt @test_moved_jumptable(i1 %tst, i32 %sw, %BigInt %l) {
68
; CHECK-LABEL: test_moved_jumptable:
79

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

3537
end:
3638
%val = phi %BigInt [ %l, %complex ], [ -1, %simple ]
39+
; Prevent SimplifyCFG from simplifying the phi node above.
40+
call void @use(%BigInt %val)
3741
ret %BigInt %val
3842
}
3943

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

+21-27
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ 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 [[COND_END_THREAD]], label [[TMP6:%.*]]
45+
; OVERIDE-NEXT: br i1 [[COND_FR]], label [[TMP5:%.*]], label [[COND_END_THREAD]]
46+
; OVERIDE: 5:
47+
; OVERIDE-NEXT: br label [[COND_END_THREAD]]
4648
; OVERIDE: cond.end.thread:
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]])
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]])
5553
; OVERIDE-NEXT: ret i32 0
5654
;
5755
entry:
@@ -90,16 +88,14 @@ define i32 @test_optsize(i32 %argc, ptr nocapture readonly %argv) local_unnamed_
9088
; DEFAULT-NEXT: [[TMP3:%.*]] = mul i32 [[CALL]], [[TMP2]]
9189
; DEFAULT-NEXT: [[TMP4:%.*]] = icmp sgt i32 [[CALL]], 0
9290
; DEFAULT-NEXT: [[COND_FR:%.*]] = freeze i1 [[TMP4]]
93-
; DEFAULT-NEXT: br i1 [[COND_FR]], label [[COND_END_THREAD]], label [[TMP6:%.*]]
91+
; DEFAULT-NEXT: br i1 [[COND_FR]], label [[TMP5:%.*]], label [[COND_END_THREAD]]
92+
; DEFAULT: 5:
93+
; DEFAULT-NEXT: br label [[COND_END_THREAD]]
9494
; DEFAULT: cond.end.thread:
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]])
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]])
10399
; DEFAULT-NEXT: ret i32 0
104100
;
105101
; OVERIDE-LABEL: @test_optsize(
@@ -115,16 +111,14 @@ define i32 @test_optsize(i32 %argc, ptr nocapture readonly %argv) local_unnamed_
115111
; OVERIDE-NEXT: [[TMP3:%.*]] = mul i32 [[CALL]], [[TMP2]]
116112
; OVERIDE-NEXT: [[TMP4:%.*]] = icmp sgt i32 [[CALL]], 0
117113
; OVERIDE-NEXT: [[COND_FR:%.*]] = freeze i1 [[TMP4]]
118-
; OVERIDE-NEXT: br i1 [[COND_FR]], label [[COND_END_THREAD]], label [[TMP6:%.*]]
114+
; OVERIDE-NEXT: br i1 [[COND_FR]], label [[TMP5:%.*]], label [[COND_END_THREAD]]
115+
; OVERIDE: 5:
116+
; OVERIDE-NEXT: br label [[COND_END_THREAD]]
119117
; OVERIDE: cond.end.thread:
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]])
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]])
128122
; OVERIDE-NEXT: ret i32 0
129123
;
130124
entry:

0 commit comments

Comments
 (0)