@@ -1249,8 +1249,11 @@ scanPHIsAndUpdateValueMap(Instruction *Prev, BasicBlock *NewBlock,
1249
1249
// instruction. Suspend instruction represented by a switch, track the PHI
1250
1250
// values and select the correct case successor when possible.
1251
1251
static bool simplifyTerminatorLeadingToRet (Instruction *InitialInst) {
1252
+ // There is nothing to simplify.
1253
+ if (isa<ReturnInst>(InitialInst))
1254
+ return false ;
1255
+
1252
1256
DenseMap<Value *, Value *> ResolvedValues;
1253
- BasicBlock *UnconditionalSucc = nullptr ;
1254
1257
assert (InitialInst->getModule ());
1255
1258
const DataLayout &DL = InitialInst->getModule ()->getDataLayout ();
1256
1259
@@ -1280,39 +1283,35 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
1280
1283
Instruction *I = InitialInst;
1281
1284
while (I->isTerminator () || isa<CmpInst>(I)) {
1282
1285
if (isa<ReturnInst>(I)) {
1283
- if (I != InitialInst) {
1284
- // If InitialInst is an unconditional branch,
1285
- // remove PHI values that come from basic block of InitialInst
1286
- if (UnconditionalSucc)
1287
- UnconditionalSucc->removePredecessor (InitialInst->getParent (), true );
1288
- ReplaceInstWithInst (InitialInst, I->clone ());
1289
- }
1286
+ ReplaceInstWithInst (InitialInst, I->clone ());
1290
1287
return true ;
1291
1288
}
1289
+
1292
1290
if (auto *BR = dyn_cast<BranchInst>(I)) {
1293
- if (BR->isUnconditional ()) {
1294
- BasicBlock *Succ = BR->getSuccessor (0 );
1295
- if (I == InitialInst)
1296
- UnconditionalSucc = Succ;
1297
- scanPHIsAndUpdateValueMap (I, Succ, ResolvedValues);
1298
- I = GetFirstValidInstruction (Succ->getFirstNonPHIOrDbgOrLifetime ());
1299
- continue ;
1291
+ unsigned SuccIndex = 0 ;
1292
+ if (BR->isConditional ()) {
1293
+ // Handle the case the condition of the conditional branch is constant.
1294
+ // e.g.,
1295
+ //
1296
+ // br i1 false, label %cleanup, label %CoroEnd
1297
+ //
1298
+ // It is possible during the transformation. We could continue the
1299
+ // simplifying in this case.
1300
+ ConstantInt *Cond = TryResolveConstant (BR->getCondition ());
1301
+ if (!Cond)
1302
+ return false ;
1303
+
1304
+ SuccIndex = Cond->isOne () ? 0 : 1 ;
1300
1305
}
1301
1306
1302
- BasicBlock *BB = BR->getParent ();
1303
- // Handle the case the condition of the conditional branch is constant.
1304
- // e.g.,
1305
- //
1306
- // br i1 false, label %cleanup, label %CoroEnd
1307
- //
1308
- // It is possible during the transformation. We could continue the
1309
- // simplifying in this case.
1310
- if (ConstantFoldTerminator (BB, /* DeleteDeadConditions=*/ true )) {
1311
- // Handle this branch in next iteration.
1312
- I = BB->getTerminator ();
1313
- continue ;
1314
- }
1315
- } else if (auto *CondCmp = dyn_cast<CmpInst>(I)) {
1307
+ BasicBlock *Succ = BR->getSuccessor (SuccIndex);
1308
+ scanPHIsAndUpdateValueMap (I, Succ, ResolvedValues);
1309
+ I = GetFirstValidInstruction (Succ->getFirstNonPHIOrDbgOrLifetime ());
1310
+
1311
+ continue ;
1312
+ }
1313
+
1314
+ if (auto *CondCmp = dyn_cast<CmpInst>(I)) {
1316
1315
// If the case number of suspended switch instruction is reduced to
1317
1316
// 1, then it is simplified to CmpInst in llvm::ConstantFoldTerminator.
1318
1317
auto *BR = dyn_cast<BranchInst>(
@@ -1336,13 +1335,14 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
1336
1335
if (!ConstResult)
1337
1336
return false ;
1338
1337
1339
- CondCmp->replaceAllUsesWith (ConstResult);
1340
- CondCmp->eraseFromParent ();
1338
+ ResolvedValues[BR->getCondition ()] = ConstResult;
1341
1339
1342
1340
// Handle this branch in next iteration.
1343
1341
I = BR;
1344
1342
continue ;
1345
- } else if (auto *SI = dyn_cast<SwitchInst>(I)) {
1343
+ }
1344
+
1345
+ if (auto *SI = dyn_cast<SwitchInst>(I)) {
1346
1346
ConstantInt *Cond = TryResolveConstant (SI->getCondition ());
1347
1347
if (!Cond)
1348
1348
return false ;
@@ -1352,9 +1352,8 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
1352
1352
I = GetFirstValidInstruction (BB->getFirstNonPHIOrDbgOrLifetime ());
1353
1353
continue ;
1354
1354
}
1355
-
1356
- return false ;
1357
1355
}
1356
+
1358
1357
return false ;
1359
1358
}
1360
1359
0 commit comments