@@ -1198,22 +1198,6 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
1198
1198
assert (InitialInst->getModule ());
1199
1199
const DataLayout &DL = InitialInst->getModule ()->getDataLayout ();
1200
1200
1201
- auto GetFirstValidInstruction = [](Instruction *I) {
1202
- while (I) {
1203
- // BitCastInst wouldn't generate actual code so that we could skip it.
1204
- if (isa<BitCastInst>(I) || I->isDebugOrPseudoInst () ||
1205
- I->isLifetimeStartOrEnd ())
1206
- I = I->getNextNode ();
1207
- else if (isInstructionTriviallyDead (I))
1208
- // Duing we are in the middle of the transformation, we need to erase
1209
- // the dead instruction manually.
1210
- I = &*I->eraseFromParent ();
1211
- else
1212
- break ;
1213
- }
1214
- return I;
1215
- };
1216
-
1217
1201
auto TryResolveConstant = [&ResolvedValues](Value *V) {
1218
1202
auto It = ResolvedValues.find (V);
1219
1203
if (It != ResolvedValues.end ())
@@ -1222,8 +1206,9 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
1222
1206
};
1223
1207
1224
1208
Instruction *I = InitialInst;
1225
- while (I-> isTerminator () || isa<CmpInst>(I) ) {
1209
+ while (true ) {
1226
1210
if (isa<ReturnInst>(I)) {
1211
+ assert (!cast<ReturnInst>(I)->getReturnValue ());
1227
1212
ReplaceInstWithInst (InitialInst, I->clone ());
1228
1213
return true ;
1229
1214
}
@@ -1247,54 +1232,48 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
1247
1232
1248
1233
BasicBlock *Succ = BR->getSuccessor (SuccIndex);
1249
1234
scanPHIsAndUpdateValueMap (I, Succ, ResolvedValues);
1250
- I = GetFirstValidInstruction (Succ->getFirstNonPHIOrDbgOrLifetime ());
1251
-
1235
+ I = Succ->getFirstNonPHIOrDbgOrLifetime ();
1252
1236
continue ;
1253
1237
}
1254
1238
1255
- if (auto *CondCmp = dyn_cast<CmpInst>(I)) {
1239
+ if (auto *Cmp = dyn_cast<CmpInst>(I)) {
1256
1240
// If the case number of suspended switch instruction is reduced to
1257
1241
// 1, then it is simplified to CmpInst in llvm::ConstantFoldTerminator.
1258
- auto *BR = dyn_cast<BranchInst>(
1259
- GetFirstValidInstruction (CondCmp->getNextNode ()));
1260
- if (!BR || !BR->isConditional () || CondCmp != BR->getCondition ())
1261
- return false ;
1262
-
1263
- // And the comparsion looks like : %cond = icmp eq i8 %V, constant.
1264
- // So we try to resolve constant for the first operand only since the
1265
- // second operand should be literal constant by design.
1266
- ConstantInt *Cond0 = TryResolveConstant (CondCmp->getOperand (0 ));
1267
- auto *Cond1 = dyn_cast<ConstantInt>(CondCmp->getOperand (1 ));
1268
- if (!Cond0 || !Cond1)
1269
- return false ;
1270
-
1271
- // Both operands of the CmpInst are Constant. So that we could evaluate
1272
- // it immediately to get the destination.
1273
- auto *ConstResult =
1274
- dyn_cast_or_null<ConstantInt>(ConstantFoldCompareInstOperands (
1275
- CondCmp->getPredicate (), Cond0, Cond1, DL));
1276
- if (!ConstResult)
1277
- return false ;
1278
-
1279
- ResolvedValues[BR->getCondition ()] = ConstResult;
1280
-
1281
- // Handle this branch in next iteration.
1282
- I = BR;
1283
- continue ;
1242
+ // Try to constant fold it.
1243
+ ConstantInt *Cond0 = TryResolveConstant (Cmp->getOperand (0 ));
1244
+ ConstantInt *Cond1 = TryResolveConstant (Cmp->getOperand (1 ));
1245
+ if (Cond0 && Cond1) {
1246
+ ConstantInt *Result =
1247
+ dyn_cast_or_null<ConstantInt>(ConstantFoldCompareInstOperands (
1248
+ Cmp->getPredicate (), Cond0, Cond1, DL));
1249
+ if (Result) {
1250
+ ResolvedValues[Cmp] = Result;
1251
+ I = I->getNextNode ();
1252
+ continue ;
1253
+ }
1254
+ }
1284
1255
}
1285
1256
1286
1257
if (auto *SI = dyn_cast<SwitchInst>(I)) {
1287
1258
ConstantInt *Cond = TryResolveConstant (SI->getCondition ());
1288
1259
if (!Cond)
1289
1260
return false ;
1290
1261
1291
- BasicBlock *BB = SI->findCaseValue (Cond)->getCaseSuccessor ();
1292
- scanPHIsAndUpdateValueMap (I, BB, ResolvedValues);
1293
- I = GetFirstValidInstruction (BB->getFirstNonPHIOrDbgOrLifetime ());
1262
+ BasicBlock *Succ = SI->findCaseValue (Cond)->getCaseSuccessor ();
1263
+ scanPHIsAndUpdateValueMap (I, Succ, ResolvedValues);
1264
+ I = Succ->getFirstNonPHIOrDbgOrLifetime ();
1265
+ continue ;
1266
+ }
1267
+
1268
+ if (I->isDebugOrPseudoInst () || I->isLifetimeStartOrEnd () ||
1269
+ wouldInstructionBeTriviallyDead (I)) {
1270
+ // We can skip instructions without side effects. If their values are
1271
+ // needed, we'll notice later, e.g. when hitting a conditional branch.
1272
+ I = I->getNextNode ();
1294
1273
continue ;
1295
1274
}
1296
1275
1297
- return false ;
1276
+ break ;
1298
1277
}
1299
1278
1300
1279
return false ;
0 commit comments