@@ -1198,22 +1198,6 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
11981198 assert (InitialInst->getModule ());
11991199 const DataLayout &DL = InitialInst->getModule ()->getDataLayout ();
12001200
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-
12171201 auto TryResolveConstant = [&ResolvedValues](Value *V) {
12181202 auto It = ResolvedValues.find (V);
12191203 if (It != ResolvedValues.end ())
@@ -1222,8 +1206,9 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
12221206 };
12231207
12241208 Instruction *I = InitialInst;
1225- while (I-> isTerminator () || isa<CmpInst>(I) ) {
1209+ while (true ) {
12261210 if (isa<ReturnInst>(I)) {
1211+ assert (!cast<ReturnInst>(I)->getReturnValue ());
12271212 ReplaceInstWithInst (InitialInst, I->clone ());
12281213 return true ;
12291214 }
@@ -1247,54 +1232,48 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
12471232
12481233 BasicBlock *Succ = BR->getSuccessor (SuccIndex);
12491234 scanPHIsAndUpdateValueMap (I, Succ, ResolvedValues);
1250- I = GetFirstValidInstruction (Succ->getFirstNonPHIOrDbgOrLifetime ());
1251-
1235+ I = Succ->getFirstNonPHIOrDbgOrLifetime ();
12521236 continue ;
12531237 }
12541238
1255- if (auto *CondCmp = dyn_cast<CmpInst>(I)) {
1239+ if (auto *Cmp = dyn_cast<CmpInst>(I)) {
12561240 // If the case number of suspended switch instruction is reduced to
12571241 // 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+ }
12841255 }
12851256
12861257 if (auto *SI = dyn_cast<SwitchInst>(I)) {
12871258 ConstantInt *Cond = TryResolveConstant (SI->getCondition ());
12881259 if (!Cond)
12891260 return false ;
12901261
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 ();
12941273 continue ;
12951274 }
12961275
1297- return false ;
1276+ break ;
12981277 }
12991278
13001279 return false ;
0 commit comments