Revert "Extend redundant block removal to blocks with non-jump instructions"

This reverts commit 987f9b6ca5f57c2afdfacf12873bdc49b360b041.

Change-Id: I9a7a2fc040dce59fae3c491b7aa07d856d256b3c
diff --git a/src/main/java/com/android/tools/r8/graph/Code.java b/src/main/java/com/android/tools/r8/graph/Code.java
index c893b40..3b5d069 100644
--- a/src/main/java/com/android/tools/r8/graph/Code.java
+++ b/src/main/java/com/android/tools/r8/graph/Code.java
@@ -257,11 +257,11 @@
     assert !callerPosition.isOutline();
     // Copy the callee frame to ensure transfer of the outline key if present.
     PositionBuilder<?, ?> newCallerBuilder =
-        outermostCallee
-            .builderWithCopy()
-            .setIsD8R8Synthesized(callerPosition.isD8R8Synthesized())
-            .setMethod(callerPosition.getMethod())
-            .setCallerPosition(callerPosition.getCallerPosition());
+        outermostCallee.builderWithCopy().setMethod(callerPosition.getMethod());
+    // Transfer the callers outer frames if any.
+    if (callerPosition.hasCallerPosition()) {
+      newCallerBuilder.setCallerPosition(callerPosition.getCallerPosition());
+    }
     // If the callee is an outline, the line must be that of the outline to maintain the positions.
     if (outermostCallee.isOutline()) {
       // This does not implement inlining an outline. The cases this hits should always be a full
diff --git a/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java b/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java
index 5924200..7250c3a 100644
--- a/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java
+++ b/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java
@@ -120,7 +120,7 @@
     return localsAtEntry;
   }
 
-  public void replaceLastInstruction(Instruction instruction) {
+  public void replaceLastInstruction(Instruction instruction, IRCode code) {
     InstructionListIterator iterator = listIterator(getInstructions().size());
     iterator.previous();
     iterator.replaceCurrentInstruction(instruction);
diff --git a/src/main/java/com/android/tools/r8/ir/code/IRCode.java b/src/main/java/com/android/tools/r8/ir/code/IRCode.java
index 3f61db3..50e4f53 100644
--- a/src/main/java/com/android/tools/r8/ir/code/IRCode.java
+++ b/src/main/java/com/android/tools/r8/ir/code/IRCode.java
@@ -886,7 +886,7 @@
 
   private boolean noRedundantBlocks() {
     for (BasicBlock block : blocks) {
-      assert !isBlockWithRedundantSuccessorBlock(block);
+      assert !isRedundantBlock(block);
     }
     return true;
   }
@@ -1315,30 +1315,7 @@
     return true;
   }
 
-  private boolean isBlockWithRedundantSuccessorBlock(BasicBlock block) {
-    if (options.debug) {
-      return isBlockWithRedundantSuccessorBlockInDebug(block);
-    } else {
-      return isBlockWithRedundantSuccessorBlockInRelease(block);
-    }
-  }
-
-  private boolean isBlockWithRedundantSuccessorBlockInRelease(BasicBlock block) {
-    if (!block.hasUniqueSuccessorWithUniquePredecessor()
-        || !block.exit().isGoto()
-        || !block.exit().getDebugValues().isEmpty()) {
-      return false;
-    }
-    BasicBlock successor = block.getUniqueSuccessor();
-    if (successor.hasCatchHandlers()) {
-      if (block.isEntry() || block.canThrow()) {
-        return false;
-      }
-    }
-    return true;
-  }
-
-  private boolean isBlockWithRedundantSuccessorBlockInDebug(BasicBlock block) {
+  private boolean isRedundantBlock(BasicBlock block) {
     return block.hasUniqueSuccessorWithUniquePredecessor()
         && block.getInstructions().size() == 1
         && block.exit().isGoto()
@@ -1347,59 +1324,31 @@
   }
 
   public void removeRedundantBlocks() {
-    // See b/237567012.
-    assert verifyNoStackInstructions();
-    Set<BasicBlock> blocksToRemove = Sets.newIdentityHashSet();
-    // Run over all blocks while merging successor blocks into the current block.
+    List<BasicBlock> blocksToRemove = new ArrayList<>();
+
     for (BasicBlock block : blocks) {
-      if (blocksToRemove.contains(block)) {
-        continue;
-      }
-      while (isBlockWithRedundantSuccessorBlock(block)) {
-        // Let the current block consume the successor.;
+      // Check that there are no redundant blocks.
+      assert !blocksToRemove.contains(block);
+      if (isRedundantBlock(block)) {
+        assert block.getUniqueSuccessor().getMutablePredecessors().size() == 1;
+        assert block.getUniqueSuccessor().getMutablePredecessors().get(0) == block;
+        assert block.getUniqueSuccessor().getPhis().size() == 0;
+        // Let the successor consume this block.
         BasicBlock successor = block.getUniqueSuccessor();
-        assert !successor.hasCatchHandlers() || !block.canThrow();
-        assert !successor.hasPhis();
-        Instruction instruction = successor.entry();
-        while (instruction != null) {
-          Instruction next = instruction.getNext();
-          if (instruction.isJumpInstruction()) {
-            block.replaceLastInstruction(instruction);
-          } else {
-            block.getInstructions().addBefore(instruction, block.exit());
-          }
-          instruction = next;
-        }
-
-        // Unlink successor block.
-        block.getMutableSuccessors().clear();
-        if (successor.hasCatchHandlers()) {
-          block.moveCatchHandlers(successor);
-        }
-        block.getMutableSuccessors().addAll(successor.getSuccessors());
-        block.forEachNormalSuccessor(
-            successorOfSuccessor -> successorOfSuccessor.replacePredecessor(successor, block));
-
-        // Clean successor block and record for removal.
-        successor.getInstructions().clear();
         successor.getMutablePredecessors().clear();
-        successor.getMutableSuccessors().clear();
-        blocksToRemove.add(successor);
+        successor.getMutablePredecessors().addAll(block.getPredecessors());
+        successor.getPhis().addAll(block.getPhis());
+        successor.getPhis().forEach(phi -> phi.setBlock(block.getUniqueSuccessor()));
+        block
+            .getPredecessors()
+            .forEach(predecessors -> predecessors.replaceSuccessor(block, successor));
+        block.getMutablePredecessors().clear();
+        block.getMutableSuccessors().clear();
+        block.getPhis().clear();
+        blocksToRemove.add(block);
       }
     }
     blocks.removeAll(blocksToRemove);
-    assert noRedundantBlocks();
-  }
-
-  private boolean verifyNoStackInstructions() {
-    for (BasicBlock block : blocks) {
-      for (Instruction instruction : block.getInstructions()) {
-        assert !instruction.isStore();
-        assert !instruction.isPop();
-        assert !instruction.hasOutValue() || !instruction.outValue().isValueOnStack();
-      }
-    }
-    return true;
   }
 
   public boolean removeAllDeadAndTrivialPhis() {
diff --git a/src/main/java/com/android/tools/r8/ir/code/InstructionList.java b/src/main/java/com/android/tools/r8/ir/code/InstructionList.java
index feb88a9..f95b67a 100644
--- a/src/main/java/com/android/tools/r8/ir/code/InstructionList.java
+++ b/src/main/java/com/android/tools/r8/ir/code/InstructionList.java
@@ -94,6 +94,7 @@
    * end.
    */
   public void addBefore(Instruction newInstruction, Instruction existingInstruction) {
+    assert newInstruction.block == null;
     if (existingInstruction != null) {
       assert linearScanFinds(existingInstruction);
     }
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/CfBuilder.java b/src/main/java/com/android/tools/r8/ir/conversion/CfBuilder.java
index 2ef3b67..b862664 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/CfBuilder.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/CfBuilder.java
@@ -171,6 +171,7 @@
       }
       timing.end();
     }
+    code.removeRedundantBlocks();
     assert code.isConsistentGraph(appView, false);
     previousPrintString =
         IRConverter.printMethodIR(
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/IRConverter.java b/src/main/java/com/android/tools/r8/ir/conversion/IRConverter.java
index 8b5d9ee..9686713 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/IRConverter.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/IRConverter.java
@@ -809,7 +809,9 @@
     if (assumeInserter != null) {
       timing.begin("Remove assume instructions");
       CodeRewriter.removeAssumeInstructions(appView, code);
+      code.removeRedundantBlocks();
       timing.end();
+      assert code.isConsistentSSA(appView);
       previous = printMethod(code, "IR after removing assume instructions (SSA)", previous);
 
       // TODO(b/214496607): Remove when dynamic types are safe w.r.t. interface assignment rules.
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/IRToDexFinalizer.java b/src/main/java/com/android/tools/r8/ir/conversion/IRToDexFinalizer.java
index 20ba9f1..84dbfb0 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/IRToDexFinalizer.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/IRToDexFinalizer.java
@@ -50,7 +50,7 @@
   }
 
   private void workaroundBugs(IRCode code, Timing timing) {
-    RuntimeWorkaroundCodeRewriter.workaroundNumberConversionRegisterAllocationBug(appView, code);
+    RuntimeWorkaroundCodeRewriter.workaroundNumberConversionRegisterAllocationBug(code, options);
     RuntimeWorkaroundCodeRewriter.workaroundDex2OatInliningIssue(appView, code);
     if (RuntimeWorkaroundCodeRewriter.workaroundInstanceOfTypeWeakeningInVerifier(appView, code)) {
       deadCodeRemover.run(code, timing);
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/passes/BranchSimplifier.java b/src/main/java/com/android/tools/r8/ir/conversion/passes/BranchSimplifier.java
index 1c19267..a72f171 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/passes/BranchSimplifier.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/passes/BranchSimplifier.java
@@ -111,11 +111,11 @@
         continue;
       }
       if (block.exit().isIf()) {
-        flipIfBranchesIfNeeded(block);
-        if (rewriteIfWithConstZero(block)) {
+        flipIfBranchesIfNeeded(code, block);
+        if (rewriteIfWithConstZero(code, block)) {
           simplified = true;
         }
-        if (rewriteIfWithObjectsIsNullOrNonNull(block)) {
+        if (rewriteIfWithObjectsIsNullOrNonNull(code, block)) {
           simplified = true;
         }
 
@@ -145,7 +145,7 @@
         if (behavioralSubsumption.isSubsumedBy(
             theIf.inValues().get(0), theIf.getPosition(),
             theIf.getTrueTarget(), theIf.fallthroughBlock())) {
-          simplifyIfWithKnownCondition(block, theIf, theIf.fallthroughBlock());
+          simplifyIfWithKnownCondition(code, block, theIf, theIf.fallthroughBlock());
           simplified = true;
         }
       }
@@ -188,6 +188,10 @@
       return this;
     }
 
+    public boolean anyAffectedValues() {
+      return anyAffectedValues;
+    }
+
     public boolean anySimplifications() {
       return anySimplifications;
     }
@@ -211,7 +215,7 @@
     if (lhsRoot.isConstNumber()) {
       ConstNumber cond = lhsRoot.getConstInstruction().asConstNumber();
       BasicBlock target = theIf.targetFromCondition(cond);
-      simplifyIfWithKnownCondition(block, theIf, target);
+      simplifyIfWithKnownCondition(code, block, theIf, target);
       return true;
     }
 
@@ -219,12 +223,12 @@
       assert theIf.getType() == IfType.EQ || theIf.getType() == IfType.NE;
 
       if (lhs.isAlwaysNull(appView)) {
-        simplifyIfWithKnownCondition(block, theIf, theIf.targetFromNullObject());
+        simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromNullObject());
         return true;
       }
 
       if (lhs.isNeverNull()) {
-        simplifyIfWithKnownCondition(block, theIf, theIf.targetFromNonNullObject());
+        simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromNonNullObject());
         return true;
       }
     }
@@ -234,7 +238,7 @@
       if (lhsAbstractValue.isConstantOrNonConstantNumberValue()
           && !lhsAbstractValue.asConstantOrNonConstantNumberValue().maybeContainsInt(0)) {
         // Value doesn't contain zero at all.
-        simplifyIfWithKnownCondition(block, theIf, theIf.targetFromCondition(1));
+        simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromCondition(1));
         return true;
       }
       if (!lhsRoot.isPhi() && lhsRoot.getDefinition().isXor()) {
@@ -243,7 +247,7 @@
         if (input != null) {
           // ifeqz !a => ifnez a
           // ifnez !a => ifeqz a
-          block.replaceLastInstruction(new If(theIf.getType().inverted(), input));
+          block.replaceLastInstruction(new If(theIf.getType().inverted(), input), code);
           return true;
         }
       }
@@ -254,7 +258,7 @@
       if (!interval.containsValue(0)) {
         // Interval doesn't contain zero at all.
         int sign = Long.signum(interval.getMin());
-        simplifyIfWithKnownCondition(block, theIf, sign);
+        simplifyIfWithKnownCondition(code, block, theIf, sign);
         return true;
       }
 
@@ -266,7 +270,7 @@
           // [a, b] < 0 is always false if a >= 0.
           // In both cases a zero condition takes the right branch.
           if (interval.getMin() == 0) {
-            simplifyIfWithKnownCondition(block, theIf, 0);
+            simplifyIfWithKnownCondition(code, block, theIf, 0);
             return true;
           }
           break;
@@ -277,7 +281,7 @@
           // [a, b] > 0 is always false if b <= 0.
           // In both cases a zero condition takes the right branch.
           if (interval.getMax() == 0) {
-            simplifyIfWithKnownCondition(block, theIf, 0);
+            simplifyIfWithKnownCondition(code, block, theIf, 0);
             return true;
           }
           break;
@@ -313,7 +317,7 @@
     Value rhsRoot = rhs.getAliasedValue();
     if (lhsRoot == rhsRoot) {
       // Comparing the same value.
-      simplifyIfWithKnownCondition(block, theIf, theIf.targetFromCondition(0));
+      simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromCondition(0));
       return true;
     }
 
@@ -321,7 +325,7 @@
         && rhsRoot.isDefinedByInstructionSatisfying(Instruction::isCreatingInstanceOrArray)) {
       // Comparing two newly created objects.
       assert theIf.getType() == IfType.EQ || theIf.getType() == IfType.NE;
-      simplifyIfWithKnownCondition(block, theIf, theIf.targetFromCondition(1));
+      simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromCondition(1));
       return true;
     }
 
@@ -330,7 +334,7 @@
       ConstNumber left = lhsRoot.getConstInstruction().asConstNumber();
       ConstNumber right = rhsRoot.getConstInstruction().asConstNumber();
       BasicBlock target = theIf.targetFromCondition(left, right);
-      simplifyIfWithKnownCondition(block, theIf, target);
+      simplifyIfWithKnownCondition(code, block, theIf, target);
       return true;
     }
 
@@ -345,7 +349,7 @@
             rhsAbstractValue.asConstantOrNonConstantNumberValue();
         if (!lhsNumberValue.mayOverlapWith(rhsNumberValue)) {
           // No overlap.
-          simplifyIfWithKnownCondition(block, theIf, 1);
+          simplifyIfWithKnownCondition(code, block, theIf, 1);
           return true;
         }
       }
@@ -360,7 +364,7 @@
       if (!leftRange.overlapsWith(rightRange)) {
         // No overlap.
         int cond = Long.signum(leftRange.getMin() - rightRange.getMin());
-        simplifyIfWithKnownCondition(block, theIf, cond);
+        simplifyIfWithKnownCondition(code, block, theIf, cond);
         return true;
       }
 
@@ -372,7 +376,7 @@
           // [a, b] >= [c, d] is always true when a == d.
           // In both cases 0 condition will choose the right branch.
           if (leftRange.getMin() == rightRange.getMax()) {
-            simplifyIfWithKnownCondition(block, theIf, 0);
+            simplifyIfWithKnownCondition(code, block, theIf, 0);
             return true;
           }
           break;
@@ -382,7 +386,7 @@
           // [a, b] <= [c, d] is always true when b == c.
           // In both cases 0 condition will choose the right branch.
           if (leftRange.getMax() == rightRange.getMin()) {
-            simplifyIfWithKnownCondition(block, theIf, 0);
+            simplifyIfWithKnownCondition(code, block, theIf, 0);
             return true;
           }
           break;
@@ -403,6 +407,7 @@
           SingleConstClassValue otherSingleConstClassValue =
               otherAbstractValue.asSingleConstClassValue();
           simplifyIfWithKnownCondition(
+              code,
               block,
               theIf,
               BooleanUtils.intValue(
@@ -418,7 +423,7 @@
           SingleFieldValue singleFieldValue = abstractValue.asSingleFieldValue();
           SingleFieldValue otherSingleFieldValue = otherAbstractValue.asSingleFieldValue();
           if (singleFieldValue.getField() == otherSingleFieldValue.getField()) {
-            simplifyIfWithKnownCondition(block, theIf, 0);
+            simplifyIfWithKnownCondition(code, block, theIf, 0);
             return true;
           }
 
@@ -429,7 +434,7 @@
             DexEncodedField otherField =
                 otherSingleFieldValue.getField().lookupOnClass(otherHolder);
             if (otherField != null && otherField.isEnum()) {
-              simplifyIfWithKnownCondition(block, theIf, 1);
+              simplifyIfWithKnownCondition(code, block, theIf, 1);
               return true;
             }
           }
@@ -440,14 +445,15 @@
     return false;
   }
 
-  private void simplifyIfWithKnownCondition(BasicBlock block, If theIf, BasicBlock target) {
+  private void simplifyIfWithKnownCondition(
+      IRCode code, BasicBlock block, If theIf, BasicBlock target) {
     BasicBlock deadTarget =
         target == theIf.getTrueTarget() ? theIf.fallthroughBlock() : theIf.getTrueTarget();
-    rewriteIfToGoto(block, theIf, target, deadTarget);
+    rewriteIfToGoto(code, block, theIf, target, deadTarget);
   }
 
-  private void simplifyIfWithKnownCondition(BasicBlock block, If theIf, int cond) {
-    simplifyIfWithKnownCondition(block, theIf, theIf.targetFromCondition(cond));
+  private void simplifyIfWithKnownCondition(IRCode code, BasicBlock block, If theIf, int cond) {
+    simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromCondition(cond));
   }
 
   /* Identify simple diamond shapes converting boolean true/false to 1/0. We consider the forms:
@@ -541,7 +547,7 @@
           // If all phis were removed, there is no need for the diamond shape anymore
           // and it can be rewritten to a goto to one of the branches.
           if (deadPhis == targetBlock.getPhis().size()) {
-            rewriteIfToGoto(block, theIf, trueBlock, falseBlock);
+            rewriteIfToGoto(code, block, theIf, trueBlock, falseBlock);
             return true;
           }
           return deadPhis > 0;
@@ -588,15 +594,15 @@
   }
 
   private void rewriteIfToGoto(
-      BasicBlock block, If theIf, BasicBlock target, BasicBlock deadTarget) {
+      IRCode code, BasicBlock block, If theIf, BasicBlock target, BasicBlock deadTarget) {
     deadTarget.unlinkSinglePredecessorSiblingsAllowed();
     assert theIf == block.exit();
-    block.replaceLastInstruction(new Goto());
+    block.replaceLastInstruction(new Goto(), code);
     assert block.exit().isGoto();
     assert block.exit().asGoto().getTarget() == target;
   }
 
-  private boolean rewriteIfWithConstZero(BasicBlock block) {
+  private boolean rewriteIfWithConstZero(IRCode code, BasicBlock block) {
     If theIf = block.exit().asIf();
     if (theIf.isZeroTest()) {
       return false;
@@ -608,13 +614,13 @@
       if (leftValue.isConstNumber()) {
         if (leftValue.getConstInstruction().asConstNumber().isZero()) {
           If ifz = new If(theIf.getType().forSwappedOperands(), rightValue);
-          block.replaceLastInstruction(ifz);
+          block.replaceLastInstruction(ifz, code);
           assert block.exit() == ifz;
           return true;
         }
       } else if (rightValue.getConstInstruction().asConstNumber().isZero()) {
         If ifz = new If(theIf.getType(), leftValue);
-        block.replaceLastInstruction(ifz);
+        block.replaceLastInstruction(ifz, code);
         assert block.exit() == ifz;
         return true;
       }
@@ -623,7 +629,7 @@
     return false;
   }
 
-  private boolean rewriteIfWithObjectsIsNullOrNonNull(BasicBlock block) {
+  private boolean rewriteIfWithObjectsIsNullOrNonNull(IRCode code, BasicBlock block) {
     If theIf = block.exit().asIf();
     if (!theIf.isZeroTest() || !theIf.getType().isEqualsOrNotEquals()) {
       return false;
@@ -635,18 +641,18 @@
       DexMethod invokedMethod = invoke.getInvokedMethod();
       if (invokedMethod.isIdenticalTo(dexItemFactory.objectsMethods.isNull)) {
         If ifz = new If(theIf.getType().inverted(), invoke.getFirstArgument());
-        block.replaceLastInstruction(ifz);
+        block.replaceLastInstruction(ifz, code);
         return true;
       } else if (invokedMethod.isIdenticalTo(dexItemFactory.objectsMethods.nonNull)) {
         If ifz = new If(theIf.getType(), invoke.getFirstArgument());
-        block.replaceLastInstruction(ifz);
+        block.replaceLastInstruction(ifz, code);
         return true;
       }
     }
     return false;
   }
 
-  private boolean flipIfBranchesIfNeeded(BasicBlock block) {
+  private boolean flipIfBranchesIfNeeded(IRCode code, BasicBlock block) {
     If theIf = block.exit().asIf();
     BasicBlock trueTarget = theIf.getTrueTarget();
     BasicBlock fallthrough = theIf.fallthroughBlock();
@@ -662,7 +668,7 @@
     // on older Android versions.
     List<Value> inValues = theIf.inValues();
     If newIf = new If(theIf.getType().inverted(), inValues);
-    block.replaceLastInstruction(newIf);
+    block.replaceLastInstruction(newIf, code);
     block.swapSuccessors(trueTarget, fallthrough);
     return true;
   }
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/passes/KotlinInlineMarkerRewriter.java b/src/main/java/com/android/tools/r8/ir/conversion/passes/KotlinInlineMarkerRewriter.java
index 909b114..34c0451 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/passes/KotlinInlineMarkerRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/passes/KotlinInlineMarkerRewriter.java
@@ -51,9 +51,6 @@
         }
       }
     }
-    if (changed) {
-      code.removeRedundantBlocks();
-    }
     return CodeRewriterResult.hasChanged(changed);
   }
 }
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/passes/NaturalIntLoopRemover.java b/src/main/java/com/android/tools/r8/ir/conversion/passes/NaturalIntLoopRemover.java
index f72c2c5..69fb225 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/passes/NaturalIntLoopRemover.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/passes/NaturalIntLoopRemover.java
@@ -48,7 +48,7 @@
     AffectedValues affectedValues = new AffectedValues();
     for (BasicBlock comparisonBlockCandidate : code.blocks) {
       if (isComparisonBlock(comparisonBlockCandidate)) {
-        loopRemoved |= tryRemoveLoop(comparisonBlockCandidate.exit().asIf(), affectedValues);
+        loopRemoved |= tryRemoveLoop(code, comparisonBlockCandidate.exit().asIf(), affectedValues);
       }
     }
     if (loopRemoved) {
@@ -84,7 +84,7 @@
     throw new Unreachable();
   }
 
-  private boolean tryRemoveLoop(If comparison, AffectedValues affectedValues) {
+  private boolean tryRemoveLoop(IRCode code, If comparison, AffectedValues affectedValues) {
     Phi loopPhi = computeLoopPhi(comparison);
     if (loopPhi == null) {
       return false;
@@ -117,7 +117,7 @@
     NaturalIntLoopWithKnowIterations loop = builder.build();
 
     if (loop.has1Iteration()) {
-      loop.remove1IterationLoop(affectedValues);
+      loop.remove1IterationLoop(code, affectedValues);
       return true;
     }
     return false;
@@ -405,19 +405,19 @@
           && target(initCounter + counterIncrement) == loopExit;
     }
 
-    private void remove1IterationLoop(AffectedValues affectedValues) {
+    private void remove1IterationLoop(IRCode code, AffectedValues affectedValues) {
       BasicBlock comparisonBlock = comparison.getBlock();
       updatePhis(comparisonBlock, affectedValues);
-      patchControlFlow(comparisonBlock);
+      patchControlFlow(code, comparisonBlock);
     }
 
-    private void patchControlFlow(BasicBlock comparisonBlock) {
+    private void patchControlFlow(IRCode code, BasicBlock comparisonBlock) {
       assert loopExit.getPhis().isEmpty(); // Edges should be split.
-      comparisonBlock.replaceLastInstruction(new Goto());
+      comparisonBlock.replaceLastInstruction(new Goto(), code);
       comparisonBlock.removeSuccessor(loopExit);
 
       backPredecessor.replaceSuccessor(comparisonBlock, loopExit);
-      backPredecessor.replaceLastInstruction(new Goto());
+      backPredecessor.replaceLastInstruction(new Goto(), code);
       comparisonBlock.removePredecessor(backPredecessor);
       loopExit.replacePredecessor(comparisonBlock, backPredecessor);
     }
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/passes/ThrowCatchOptimizer.java b/src/main/java/com/android/tools/r8/ir/conversion/passes/ThrowCatchOptimizer.java
index 4e6eb88..23a66a3 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/passes/ThrowCatchOptimizer.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/passes/ThrowCatchOptimizer.java
@@ -361,9 +361,7 @@
     }
     if (hasUnlinkedCatchHandlers) {
       code.removeUnreachableBlocks();
-      code.removeRedundantBlocks();
     }
-    assert code.isConsistentSSA(appView);
   }
 
   private boolean isSingleHandlerTrivial(BasicBlock firstBlock, IRCode code) {
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
index 9629859..3438304 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
@@ -180,7 +180,6 @@
         }
       }
     }
-    code.removeRedundantBlocks();
     assert code.isConsistentSSA(appView);
   }
 
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/ConstantCanonicalizer.java b/src/main/java/com/android/tools/r8/ir/optimize/ConstantCanonicalizer.java
index 4633083..db5c9dc 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/ConstantCanonicalizer.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/ConstantCanonicalizer.java
@@ -617,7 +617,9 @@
     assert insertionPoint.isPhi() || instructionIterator.peekPrevious() == insertionPoint;
     newInstruction.setPosition(
         getPositionForCanonicalizationConstantAtInsertionPoint(insertionPoint, newInstruction));
-    if (newInstruction.instructionTypeCanThrow() && insertionPoint.getBlock().hasCatchHandlers()) {
+    if (newInstruction.instructionTypeCanThrow()
+        && insertionPoint.getBlock().hasCatchHandlers()
+        && insertionPoint.getBlock().canThrow()) {
       // Split the block and rewind the block iterator to the insertion block.
       BasicBlock splitBlock =
           instructionIterator.splitCopyCatchHandlers(
@@ -636,6 +638,7 @@
           assert !splitBlock.canThrow();
           splitBlock.listIterator().add(newInstruction);
         } else {
+          assert splitBlock.canThrow();
           instructionIterator.addBeforeAndPositionBeforeNewInstruction(newInstruction);
         }
         instructionIterator.positionAfterPreviousInstruction(insertionPoint.asInstruction());
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/RuntimeWorkaroundCodeRewriter.java b/src/main/java/com/android/tools/r8/ir/optimize/RuntimeWorkaroundCodeRewriter.java
index 0756ee0..2262154 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/RuntimeWorkaroundCodeRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/RuntimeWorkaroundCodeRewriter.java
@@ -294,12 +294,12 @@
 
   // See comment for InternalOptions.canHaveNumberConversionRegisterAllocationBug().
   public static void workaroundNumberConversionRegisterAllocationBug(
-      AppView<?> appView, IRCode code) {
-    if (!appView.options().canHaveNumberConversionRegisterAllocationBug()) {
+      IRCode code, InternalOptions options) {
+    if (!options.canHaveNumberConversionRegisterAllocationBug()) {
       return;
     }
 
-    DexItemFactory dexItemFactory = appView.dexItemFactory();
+    DexItemFactory dexItemFactory = options.dexItemFactory();
     ListIterator<BasicBlock> blocks = code.listIterator();
     while (blocks.hasNext()) {
       BasicBlock block = blocks.next();
@@ -345,8 +345,6 @@
         }
       }
     }
-    code.removeRedundantBlocks();
-    assert code.isConsistentSSA(appView);
   }
 
   private static void ensureInstructionBefore(
diff --git a/src/main/java/com/android/tools/r8/naming/IdentifierNameStringMarker.java b/src/main/java/com/android/tools/r8/naming/IdentifierNameStringMarker.java
index ac6868b..4417b2a 100644
--- a/src/main/java/com/android/tools/r8/naming/IdentifierNameStringMarker.java
+++ b/src/main/java/com/android/tools/r8/naming/IdentifierNameStringMarker.java
@@ -155,9 +155,6 @@
         }
       }
     }
-    if (result.hasChanged().isPossiblyTrue()) {
-      code.removeRedundantBlocks();
-    }
     return result;
   }
 
diff --git a/src/test/java/com/android/tools/r8/ir/IrInjectionTestBase.java b/src/test/java/com/android/tools/r8/ir/IrInjectionTestBase.java
index d558a57..550453a 100644
--- a/src/test/java/com/android/tools/r8/ir/IrInjectionTestBase.java
+++ b/src/test/java/com/android/tools/r8/ir/IrInjectionTestBase.java
@@ -128,6 +128,7 @@
     }
 
     public String run() throws IOException {
+      Timing timing = Timing.empty();
       IRConverter converter = new IRConverter(appView);
       code.removeRedundantBlocks();
       converter.replaceCodeForTesting(code);
diff --git a/src/test/java/com/android/tools/r8/ir/SplitBlockTest.java b/src/test/java/com/android/tools/r8/ir/SplitBlockTest.java
index 4db7196..a5032c9 100644
--- a/src/test/java/com/android/tools/r8/ir/SplitBlockTest.java
+++ b/src/test/java/com/android/tools/r8/ir/SplitBlockTest.java
@@ -5,7 +5,6 @@
 package com.android.tools.r8.ir;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
@@ -100,11 +99,11 @@
       assertEquals(argumentInstructions, test.countArgumentInstructions());
       assertEquals(firstBlockInstructions, block.getInstructions().size());
       InstructionList instructionList = block.getInstructions();
-      assertFalse(instructionList.getNth(i).isArgument());
+      assertTrue(!instructionList.getNth(i).isArgument());
 
       InstructionListIterator iterator = test.listIteratorAt(block, i);
       BasicBlock newBlock = iterator.split(code);
-      assertTrue(code.isConsistentSSAAllowingRedundantBlocks(appView));
+      assertTrue(code.isConsistentSSA(appView));
 
       assertEquals(initialBlockCount + 1, code.blocks.size());
       assertEquals(i + 1, code.entryBlock().getInstructions().size());
@@ -136,11 +135,11 @@
       assertEquals(argumentInstructions, test.countArgumentInstructions());
       assertEquals(firstBlockInstructions, block.getInstructions().size());
       InstructionList instructionList = block.getInstructions();
-      assertFalse(instructionList.getNth(i).isArgument());
+      assertTrue(!instructionList.getNth(i).isArgument());
 
       InstructionListIterator iterator = test.listIteratorAt(block, i);
       BasicBlock newBlock = iterator.split(code, 1);
-      assertTrue(code.isConsistentSSAAllowingRedundantBlocks(appView));
+      assertTrue(code.isConsistentSSA(appView));
 
       assertEquals(initialBlockCount + 2, code.blocks.size());
       assertEquals(i + 1, code.entryBlock().getInstructions().size());
@@ -342,11 +341,11 @@
       assertEquals(argumentInstructions, test.countArgumentInstructions());
       assertEquals(firstBlockInstructions, block.getInstructions().size());
       InstructionList instructionList = block.getInstructions();
-      assertFalse(instructionList.getNth(i).isArgument());
+      assertTrue(!instructionList.getNth(i).isArgument());
 
       InstructionListIterator iterator = test.listIteratorAt(block, i);
       BasicBlock newBlock = iterator.split(code);
-      assertTrue(code.isConsistentSSAAllowingRedundantBlocks(appView));
+      assertTrue(code.isConsistentSSA(appView));
 
       assertEquals(initialBlockCount + 1, code.blocks.size());
       assertEquals(i + 1, code.entryBlock().getInstructions().size());
@@ -465,11 +464,11 @@
       assertEquals(argumentInstructions, test.countArgumentInstructions());
       assertEquals(firstBlockInstructions, block.getInstructions().size());
       InstructionList instructionList = block.getInstructions();
-      assertFalse(instructionList.getNth(i).isArgument());
+      assertTrue(!instructionList.getNth(i).isArgument());
 
       InstructionListIterator iterator = test.listIteratorAt(block, i);
       BasicBlock newBlock = iterator.split(code);
-      assertTrue(code.isConsistentSSAAllowingRedundantBlocks(appView));
+      assertTrue(code.isConsistentSSA(appView));
 
       assertEquals(initialBlockCount + 1, code.blocks.size());
       assertEquals(i + 1, code.entryBlock().getInstructions().size());
diff --git a/src/test/java/com/android/tools/r8/kotlin/SimplifyIfNotNullKotlinTest.java b/src/test/java/com/android/tools/r8/kotlin/SimplifyIfNotNullKotlinTest.java
index b86c119..da53eb3 100644
--- a/src/test/java/com/android/tools/r8/kotlin/SimplifyIfNotNullKotlinTest.java
+++ b/src/test/java/com/android/tools/r8/kotlin/SimplifyIfNotNullKotlinTest.java
@@ -75,7 +75,7 @@
               long paramNullCheckCount =
                   countCall(testMethod, "Intrinsics", "checkParameterIsNotNull");
               // One after Iterator#hasNext, and another in the filter predicate: sinceYear != null.
-              assertEquals(testParameters.isCfRuntime() ? 5 : 2, ifzCount);
+              assertEquals(2, ifzCount);
               assertEquals(0, paramNullCheckCount);
             });
   }
diff --git a/src/test/java/com/android/tools/r8/kotlin/lambda/KotlinLambdaMergingKeepAttributesKotlinStyleTest.java b/src/test/java/com/android/tools/r8/kotlin/lambda/KotlinLambdaMergingKeepAttributesKotlinStyleTest.java
index d0faa2b..8f359b5 100644
--- a/src/test/java/com/android/tools/r8/kotlin/lambda/KotlinLambdaMergingKeepAttributesKotlinStyleTest.java
+++ b/src/test/java/com/android/tools/r8/kotlin/lambda/KotlinLambdaMergingKeepAttributesKotlinStyleTest.java
@@ -105,6 +105,12 @@
         inspector
             .assertIsCompleteMergeGroup(
                 lambdasInInput.getKStyleLambdaReferenceFromTypeName(
+                    getTestName(), "MainKt$testFirst$1"),
+                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
+                    getTestName(), "MainKt$testFirst$2"),
+                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
+                    getTestName(), "MainKt$testFirst$3"),
+                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
                     getTestName(), "MainKt$testFirst$4"),
                 lambdasInInput.getKStyleLambdaReferenceFromTypeName(
                     getTestName(), "MainKt$testFirst$5"),
@@ -117,6 +123,12 @@
                 lambdasInInput.getKStyleLambdaReferenceFromTypeName(
                     getTestName(), "MainKt$testFirst$9"),
                 lambdasInInput.getKStyleLambdaReferenceFromTypeName(
+                    getTestName(), "MainKt$testSecond$1"),
+                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
+                    getTestName(), "MainKt$testSecond$2"),
+                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
+                    getTestName(), "MainKt$testSecond$3"),
+                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
                     getTestName(), "MainKt$testSecond$4"),
                 lambdasInInput.getKStyleLambdaReferenceFromTypeName(
                     getTestName(), "MainKt$testSecond$5"),
diff --git a/src/test/java/com/android/tools/r8/repackage/MappingFileAfterRepackagingTest.java b/src/test/java/com/android/tools/r8/repackage/MappingFileAfterRepackagingTest.java
index 2c53eb2..1464cff 100644
--- a/src/test/java/com/android/tools/r8/repackage/MappingFileAfterRepackagingTest.java
+++ b/src/test/java/com/android/tools/r8/repackage/MappingFileAfterRepackagingTest.java
@@ -59,18 +59,14 @@
                               line.contains(
                                   "java.lang.String MappingFileAfterRepackagingTest$A.toString()"))
                       .count();
-              assertEquals(
-                  repackage ? 1 + BooleanUtils.intValue(parameters.isDexRuntime()) : 0,
-                  syntheticMatches);
+              assertEquals(repackage ? 2 : 0, syntheticMatches);
 
               long unqualifiedMatches =
                   StringUtils.splitLines(runResult.proguardMap()).stream()
                       .filter(line -> line.contains("java.lang.String toString()"))
                       .count();
               assertEquals(
-                  (repackage ? 1 : 2 + BooleanUtils.intValue(parameters.isDexRuntime()))
-                      + BooleanUtils.intValue(isPc2pc()),
-                  unqualifiedMatches);
+                  (repackage ? 1 : 3) + BooleanUtils.intValue(isPc2pc()), unqualifiedMatches);
             });
   }
 
@@ -82,7 +78,7 @@
   static class Main {
 
     public static void main(String[] args) {
-      System.out.println(System.currentTimeMillis() > 0 ? new A() : new B());
+      System.out.println((System.currentTimeMillis() > 0 ? new A() : new B()));
     }
   }