@@ -847,17 +847,17 @@ static bool CanMergeValues(Value *First, Value *Second) {
847
847
// / branch to Succ, into Succ.
848
848
// /
849
849
// / 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) {
853
851
assert (*succ_begin (BB) == Succ && " Succ is not successor of BB!" );
854
852
855
853
LLVM_DEBUG (dbgs () << " Looking to fold " << BB->getName () << " into "
856
854
<< Succ->getName () << " \n " );
857
855
// Shortcut, if there is only a single predecessor it must be BB and merging
858
856
// 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));
861
861
862
862
// Look at all the phi nodes in Succ, to see if they present a conflict when
863
863
// merging these blocks
@@ -997,47 +997,16 @@ static void replaceUndefValuesInPhi(PHINode *PN,
997
997
}
998
998
}
999
999
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
-
1029
1000
// / Replace a value flowing from a block to a phi with
1030
1001
// / potentially multiple instances of that value flowing from the
1031
1002
// / block's predecessors to the phi.
1032
1003
// /
1033
1004
// / \param BB The block with the value flowing into the phi.
1034
1005
// / \param BBPreds The predecessors of BB.
1035
1006
// / \param PN The phi that we are updating.
1036
- // / \param CommonPred The common predecessor of BB and PN's BasicBlock
1037
1007
static void redirectValuesFromPredecessorsToPhi (BasicBlock *BB,
1038
1008
const PredBlockVector &BBPreds,
1039
- PHINode *PN,
1040
- BasicBlock *CommonPred) {
1009
+ PHINode *PN) {
1041
1010
Value *OldVal = PN->removeIncomingValue (BB, false );
1042
1011
assert (OldVal && " No entry in PHI for Pred BB!" );
1043
1012
@@ -1065,39 +1034,26 @@ static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
1065
1034
// will trigger asserts if we try to clean it up now, without also
1066
1035
// simplifying the corresponding conditional branch).
1067
1036
BasicBlock *PredBB = OldValPN->getIncomingBlock (i);
1068
-
1069
- if (PredBB == CommonPred)
1070
- continue ;
1071
-
1072
1037
Value *PredVal = OldValPN->getIncomingValue (i);
1073
- Value *Selected =
1074
- selectIncomingValueForBlock (PredVal, PredBB, IncomingValues);
1038
+ Value *Selected = selectIncomingValueForBlock (PredVal, PredBB,
1039
+ IncomingValues);
1075
1040
1076
1041
// And add a new incoming value for this predecessor for the
1077
1042
// newly retargeted branch.
1078
1043
PN->addIncoming (Selected, PredBB);
1079
1044
}
1080
- if (CommonPred)
1081
- PN->addIncoming (OldValPN->getIncomingValueForBlock (CommonPred), BB);
1082
-
1083
1045
} else {
1084
1046
for (unsigned i = 0 , e = BBPreds.size (); i != e; ++i) {
1085
1047
// Update existing incoming values in PN for this
1086
1048
// predecessor of BB.
1087
1049
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);
1094
1052
1095
1053
// And add a new incoming value for this predecessor for the
1096
1054
// newly retargeted branch.
1097
1055
PN->addIncoming (Selected, PredBB);
1098
1056
}
1099
- if (CommonPred)
1100
- PN->addIncoming (OldVal, BB);
1101
1057
}
1102
1058
1103
1059
replaceUndefValuesInPhi (PN, IncomingValues);
@@ -1108,30 +1064,13 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
1108
1064
assert (BB != &BB->getParent ()->getEntryBlock () &&
1109
1065
" TryToSimplifyUncondBranchFromEmptyBlock called on entry block!" );
1110
1066
1111
- // We can't simplify infinite loops.
1067
+ // We can't eliminate infinite loops.
1112
1068
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 ;
1118
1070
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 ;
1135
1074
1136
1075
// Check for cases where Succ has multiple predecessors and a PHI node in BB
1137
1076
// has uses which will not disappear when the PHI nodes are merged. It is
@@ -1160,11 +1099,6 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
1160
1099
}
1161
1100
}
1162
1101
1163
- if (BBPhisMergeable && CommonPred)
1164
- LLVM_DEBUG (dbgs () << " Found Common Predecessor between: " << BB->getName ()
1165
- << " and " << Succ->getName () << " : "
1166
- << CommonPred->getName () << " \n " );
1167
-
1168
1102
// 'BB' and 'BB->Pred' are loop latches, bail out to presrve inner loop
1169
1103
// metadata.
1170
1104
//
@@ -1237,37 +1171,25 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
1237
1171
if (PredTI->hasMetadata (LLVMContext::MD_loop))
1238
1172
return false ;
1239
1173
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);
1244
1175
1245
1176
SmallVector<DominatorTree::UpdateType, 32 > Updates;
1246
-
1247
1177
if (DTU) {
1248
1178
// To avoid processing the same predecessor more than once.
1249
1179
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));
1252
1182
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))
1257
1186
if (SeenPreds.insert (PredOfBB).second )
1258
1187
Updates.push_back ({DominatorTree::Insert, PredOfBB, Succ});
1259
- }
1260
-
1261
1188
SeenPreds.clear ();
1262
-
1263
1189
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 )
1267
1191
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});
1271
1193
}
1272
1194
1273
1195
if (isa<PHINode>(Succ->begin ())) {
@@ -1279,19 +1201,21 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
1279
1201
// Loop over all of the PHI nodes in the successor of BB.
1280
1202
for (BasicBlock::iterator I = Succ->begin (); isa<PHINode>(I); ++I) {
1281
1203
PHINode *PN = cast<PHINode>(I);
1282
- redirectValuesFromPredecessorsToPhi (BB, BBPreds, PN, CommonPred);
1204
+
1205
+ redirectValuesFromPredecessorsToPhi (BB, BBPreds, PN);
1283
1206
}
1284
1207
}
1285
1208
1286
1209
if (Succ->getSinglePredecessor ()) {
1287
1210
// BB is the only predecessor of Succ, so Succ will end up with exactly
1288
1211
// the same predecessors BB had.
1212
+
1289
1213
// Copy over any phi, debug or lifetime instruction.
1290
1214
BB->getTerminator ()->eraseFromParent ();
1291
1215
Succ->splice (Succ->getFirstNonPHI ()->getIterator (), BB);
1292
1216
} else {
1293
1217
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 .
1295
1219
assert (PN->use_empty () && " There shouldn't be any uses here!" );
1296
1220
PN->eraseFromParent ();
1297
1221
}
@@ -1304,35 +1228,21 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
1304
1228
for (BasicBlock *Pred : predecessors (BB))
1305
1229
Pred->getTerminator ()->setMetadata (LLVMContext::MD_loop, LoopMD);
1306
1230
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." );
1330
1241
1331
1242
if (DTU)
1332
1243
DTU->applyUpdates (Updates);
1333
1244
1334
- if (BBKillable)
1335
- DeleteDeadBlock (BB, DTU);
1245
+ DeleteDeadBlock (BB, DTU);
1336
1246
1337
1247
return true ;
1338
1248
}
0 commit comments