Minor cleanup for trivial goto removal
Change-Id: I3e9fb7e324ec3e7d1471fe71e217b092850ddcd2
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 7250c3a..5924200 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, IRCode code) {
+ public void replaceLastInstruction(Instruction instruction) {
InstructionListIterator iterator = listIterator(getInstructions().size());
iterator.previous();
iterator.replaceCurrentInstruction(instruction);
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 f95b67a..feb88a9 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,7 +94,6 @@
* 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 b862664..2ef3b67 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,7 +171,6 @@
}
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 9686713..8b5d9ee 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,9 +809,7 @@
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/passes/BranchSimplifier.java b/src/main/java/com/android/tools/r8/ir/conversion/passes/BranchSimplifier.java
index a72f171..1c19267 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(code, block);
- if (rewriteIfWithConstZero(code, block)) {
+ flipIfBranchesIfNeeded(block);
+ if (rewriteIfWithConstZero(block)) {
simplified = true;
}
- if (rewriteIfWithObjectsIsNullOrNonNull(code, block)) {
+ if (rewriteIfWithObjectsIsNullOrNonNull(block)) {
simplified = true;
}
@@ -145,7 +145,7 @@
if (behavioralSubsumption.isSubsumedBy(
theIf.inValues().get(0), theIf.getPosition(),
theIf.getTrueTarget(), theIf.fallthroughBlock())) {
- simplifyIfWithKnownCondition(code, block, theIf, theIf.fallthroughBlock());
+ simplifyIfWithKnownCondition(block, theIf, theIf.fallthroughBlock());
simplified = true;
}
}
@@ -188,10 +188,6 @@
return this;
}
- public boolean anyAffectedValues() {
- return anyAffectedValues;
- }
-
public boolean anySimplifications() {
return anySimplifications;
}
@@ -215,7 +211,7 @@
if (lhsRoot.isConstNumber()) {
ConstNumber cond = lhsRoot.getConstInstruction().asConstNumber();
BasicBlock target = theIf.targetFromCondition(cond);
- simplifyIfWithKnownCondition(code, block, theIf, target);
+ simplifyIfWithKnownCondition(block, theIf, target);
return true;
}
@@ -223,12 +219,12 @@
assert theIf.getType() == IfType.EQ || theIf.getType() == IfType.NE;
if (lhs.isAlwaysNull(appView)) {
- simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromNullObject());
+ simplifyIfWithKnownCondition(block, theIf, theIf.targetFromNullObject());
return true;
}
if (lhs.isNeverNull()) {
- simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromNonNullObject());
+ simplifyIfWithKnownCondition(block, theIf, theIf.targetFromNonNullObject());
return true;
}
}
@@ -238,7 +234,7 @@
if (lhsAbstractValue.isConstantOrNonConstantNumberValue()
&& !lhsAbstractValue.asConstantOrNonConstantNumberValue().maybeContainsInt(0)) {
// Value doesn't contain zero at all.
- simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromCondition(1));
+ simplifyIfWithKnownCondition(block, theIf, theIf.targetFromCondition(1));
return true;
}
if (!lhsRoot.isPhi() && lhsRoot.getDefinition().isXor()) {
@@ -247,7 +243,7 @@
if (input != null) {
// ifeqz !a => ifnez a
// ifnez !a => ifeqz a
- block.replaceLastInstruction(new If(theIf.getType().inverted(), input), code);
+ block.replaceLastInstruction(new If(theIf.getType().inverted(), input));
return true;
}
}
@@ -258,7 +254,7 @@
if (!interval.containsValue(0)) {
// Interval doesn't contain zero at all.
int sign = Long.signum(interval.getMin());
- simplifyIfWithKnownCondition(code, block, theIf, sign);
+ simplifyIfWithKnownCondition(block, theIf, sign);
return true;
}
@@ -270,7 +266,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(code, block, theIf, 0);
+ simplifyIfWithKnownCondition(block, theIf, 0);
return true;
}
break;
@@ -281,7 +277,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(code, block, theIf, 0);
+ simplifyIfWithKnownCondition(block, theIf, 0);
return true;
}
break;
@@ -317,7 +313,7 @@
Value rhsRoot = rhs.getAliasedValue();
if (lhsRoot == rhsRoot) {
// Comparing the same value.
- simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromCondition(0));
+ simplifyIfWithKnownCondition(block, theIf, theIf.targetFromCondition(0));
return true;
}
@@ -325,7 +321,7 @@
&& rhsRoot.isDefinedByInstructionSatisfying(Instruction::isCreatingInstanceOrArray)) {
// Comparing two newly created objects.
assert theIf.getType() == IfType.EQ || theIf.getType() == IfType.NE;
- simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromCondition(1));
+ simplifyIfWithKnownCondition(block, theIf, theIf.targetFromCondition(1));
return true;
}
@@ -334,7 +330,7 @@
ConstNumber left = lhsRoot.getConstInstruction().asConstNumber();
ConstNumber right = rhsRoot.getConstInstruction().asConstNumber();
BasicBlock target = theIf.targetFromCondition(left, right);
- simplifyIfWithKnownCondition(code, block, theIf, target);
+ simplifyIfWithKnownCondition(block, theIf, target);
return true;
}
@@ -349,7 +345,7 @@
rhsAbstractValue.asConstantOrNonConstantNumberValue();
if (!lhsNumberValue.mayOverlapWith(rhsNumberValue)) {
// No overlap.
- simplifyIfWithKnownCondition(code, block, theIf, 1);
+ simplifyIfWithKnownCondition(block, theIf, 1);
return true;
}
}
@@ -364,7 +360,7 @@
if (!leftRange.overlapsWith(rightRange)) {
// No overlap.
int cond = Long.signum(leftRange.getMin() - rightRange.getMin());
- simplifyIfWithKnownCondition(code, block, theIf, cond);
+ simplifyIfWithKnownCondition(block, theIf, cond);
return true;
}
@@ -376,7 +372,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(code, block, theIf, 0);
+ simplifyIfWithKnownCondition(block, theIf, 0);
return true;
}
break;
@@ -386,7 +382,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(code, block, theIf, 0);
+ simplifyIfWithKnownCondition(block, theIf, 0);
return true;
}
break;
@@ -407,7 +403,6 @@
SingleConstClassValue otherSingleConstClassValue =
otherAbstractValue.asSingleConstClassValue();
simplifyIfWithKnownCondition(
- code,
block,
theIf,
BooleanUtils.intValue(
@@ -423,7 +418,7 @@
SingleFieldValue singleFieldValue = abstractValue.asSingleFieldValue();
SingleFieldValue otherSingleFieldValue = otherAbstractValue.asSingleFieldValue();
if (singleFieldValue.getField() == otherSingleFieldValue.getField()) {
- simplifyIfWithKnownCondition(code, block, theIf, 0);
+ simplifyIfWithKnownCondition(block, theIf, 0);
return true;
}
@@ -434,7 +429,7 @@
DexEncodedField otherField =
otherSingleFieldValue.getField().lookupOnClass(otherHolder);
if (otherField != null && otherField.isEnum()) {
- simplifyIfWithKnownCondition(code, block, theIf, 1);
+ simplifyIfWithKnownCondition(block, theIf, 1);
return true;
}
}
@@ -445,15 +440,14 @@
return false;
}
- private void simplifyIfWithKnownCondition(
- IRCode code, BasicBlock block, If theIf, BasicBlock target) {
+ private void simplifyIfWithKnownCondition(BasicBlock block, If theIf, BasicBlock target) {
BasicBlock deadTarget =
target == theIf.getTrueTarget() ? theIf.fallthroughBlock() : theIf.getTrueTarget();
- rewriteIfToGoto(code, block, theIf, target, deadTarget);
+ rewriteIfToGoto(block, theIf, target, deadTarget);
}
- private void simplifyIfWithKnownCondition(IRCode code, BasicBlock block, If theIf, int cond) {
- simplifyIfWithKnownCondition(code, block, theIf, theIf.targetFromCondition(cond));
+ private void simplifyIfWithKnownCondition(BasicBlock block, If theIf, int cond) {
+ simplifyIfWithKnownCondition(block, theIf, theIf.targetFromCondition(cond));
}
/* Identify simple diamond shapes converting boolean true/false to 1/0. We consider the forms:
@@ -547,7 +541,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(code, block, theIf, trueBlock, falseBlock);
+ rewriteIfToGoto(block, theIf, trueBlock, falseBlock);
return true;
}
return deadPhis > 0;
@@ -594,15 +588,15 @@
}
private void rewriteIfToGoto(
- IRCode code, BasicBlock block, If theIf, BasicBlock target, BasicBlock deadTarget) {
+ BasicBlock block, If theIf, BasicBlock target, BasicBlock deadTarget) {
deadTarget.unlinkSinglePredecessorSiblingsAllowed();
assert theIf == block.exit();
- block.replaceLastInstruction(new Goto(), code);
+ block.replaceLastInstruction(new Goto());
assert block.exit().isGoto();
assert block.exit().asGoto().getTarget() == target;
}
- private boolean rewriteIfWithConstZero(IRCode code, BasicBlock block) {
+ private boolean rewriteIfWithConstZero(BasicBlock block) {
If theIf = block.exit().asIf();
if (theIf.isZeroTest()) {
return false;
@@ -614,13 +608,13 @@
if (leftValue.isConstNumber()) {
if (leftValue.getConstInstruction().asConstNumber().isZero()) {
If ifz = new If(theIf.getType().forSwappedOperands(), rightValue);
- block.replaceLastInstruction(ifz, code);
+ block.replaceLastInstruction(ifz);
assert block.exit() == ifz;
return true;
}
} else if (rightValue.getConstInstruction().asConstNumber().isZero()) {
If ifz = new If(theIf.getType(), leftValue);
- block.replaceLastInstruction(ifz, code);
+ block.replaceLastInstruction(ifz);
assert block.exit() == ifz;
return true;
}
@@ -629,7 +623,7 @@
return false;
}
- private boolean rewriteIfWithObjectsIsNullOrNonNull(IRCode code, BasicBlock block) {
+ private boolean rewriteIfWithObjectsIsNullOrNonNull(BasicBlock block) {
If theIf = block.exit().asIf();
if (!theIf.isZeroTest() || !theIf.getType().isEqualsOrNotEquals()) {
return false;
@@ -641,18 +635,18 @@
DexMethod invokedMethod = invoke.getInvokedMethod();
if (invokedMethod.isIdenticalTo(dexItemFactory.objectsMethods.isNull)) {
If ifz = new If(theIf.getType().inverted(), invoke.getFirstArgument());
- block.replaceLastInstruction(ifz, code);
+ block.replaceLastInstruction(ifz);
return true;
} else if (invokedMethod.isIdenticalTo(dexItemFactory.objectsMethods.nonNull)) {
If ifz = new If(theIf.getType(), invoke.getFirstArgument());
- block.replaceLastInstruction(ifz, code);
+ block.replaceLastInstruction(ifz);
return true;
}
}
return false;
}
- private boolean flipIfBranchesIfNeeded(IRCode code, BasicBlock block) {
+ private boolean flipIfBranchesIfNeeded(BasicBlock block) {
If theIf = block.exit().asIf();
BasicBlock trueTarget = theIf.getTrueTarget();
BasicBlock fallthrough = theIf.fallthroughBlock();
@@ -668,7 +662,7 @@
// on older Android versions.
List<Value> inValues = theIf.inValues();
If newIf = new If(theIf.getType().inverted(), inValues);
- block.replaceLastInstruction(newIf, code);
+ block.replaceLastInstruction(newIf);
block.swapSuccessors(trueTarget, fallthrough);
return true;
}
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 69fb225..f72c2c5 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(code, comparisonBlockCandidate.exit().asIf(), affectedValues);
+ loopRemoved |= tryRemoveLoop(comparisonBlockCandidate.exit().asIf(), affectedValues);
}
}
if (loopRemoved) {
@@ -84,7 +84,7 @@
throw new Unreachable();
}
- private boolean tryRemoveLoop(IRCode code, If comparison, AffectedValues affectedValues) {
+ private boolean tryRemoveLoop(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(code, affectedValues);
+ loop.remove1IterationLoop(affectedValues);
return true;
}
return false;
@@ -405,19 +405,19 @@
&& target(initCounter + counterIncrement) == loopExit;
}
- private void remove1IterationLoop(IRCode code, AffectedValues affectedValues) {
+ private void remove1IterationLoop(AffectedValues affectedValues) {
BasicBlock comparisonBlock = comparison.getBlock();
updatePhis(comparisonBlock, affectedValues);
- patchControlFlow(code, comparisonBlock);
+ patchControlFlow(comparisonBlock);
}
- private void patchControlFlow(IRCode code, BasicBlock comparisonBlock) {
+ private void patchControlFlow(BasicBlock comparisonBlock) {
assert loopExit.getPhis().isEmpty(); // Edges should be split.
- comparisonBlock.replaceLastInstruction(new Goto(), code);
+ comparisonBlock.replaceLastInstruction(new Goto());
comparisonBlock.removeSuccessor(loopExit);
backPredecessor.replaceSuccessor(comparisonBlock, loopExit);
- backPredecessor.replaceLastInstruction(new Goto(), code);
+ backPredecessor.replaceLastInstruction(new Goto());
comparisonBlock.removePredecessor(backPredecessor);
loopExit.replacePredecessor(comparisonBlock, backPredecessor);
}
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 550453a..d558a57 100644
--- a/src/test/java/com/android/tools/r8/ir/IrInjectionTestBase.java
+++ b/src/test/java/com/android/tools/r8/ir/IrInjectionTestBase.java
@@ -128,7 +128,6 @@
}
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 a5032c9..4db7196 100644
--- a/src/test/java/com/android/tools/r8/ir/SplitBlockTest.java
+++ b/src/test/java/com/android/tools/r8/ir/SplitBlockTest.java
@@ -5,6 +5,7 @@
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;
@@ -99,11 +100,11 @@
assertEquals(argumentInstructions, test.countArgumentInstructions());
assertEquals(firstBlockInstructions, block.getInstructions().size());
InstructionList instructionList = block.getInstructions();
- assertTrue(!instructionList.getNth(i).isArgument());
+ assertFalse(instructionList.getNth(i).isArgument());
InstructionListIterator iterator = test.listIteratorAt(block, i);
BasicBlock newBlock = iterator.split(code);
- assertTrue(code.isConsistentSSA(appView));
+ assertTrue(code.isConsistentSSAAllowingRedundantBlocks(appView));
assertEquals(initialBlockCount + 1, code.blocks.size());
assertEquals(i + 1, code.entryBlock().getInstructions().size());
@@ -135,11 +136,11 @@
assertEquals(argumentInstructions, test.countArgumentInstructions());
assertEquals(firstBlockInstructions, block.getInstructions().size());
InstructionList instructionList = block.getInstructions();
- assertTrue(!instructionList.getNth(i).isArgument());
+ assertFalse(instructionList.getNth(i).isArgument());
InstructionListIterator iterator = test.listIteratorAt(block, i);
BasicBlock newBlock = iterator.split(code, 1);
- assertTrue(code.isConsistentSSA(appView));
+ assertTrue(code.isConsistentSSAAllowingRedundantBlocks(appView));
assertEquals(initialBlockCount + 2, code.blocks.size());
assertEquals(i + 1, code.entryBlock().getInstructions().size());
@@ -341,11 +342,11 @@
assertEquals(argumentInstructions, test.countArgumentInstructions());
assertEquals(firstBlockInstructions, block.getInstructions().size());
InstructionList instructionList = block.getInstructions();
- assertTrue(!instructionList.getNth(i).isArgument());
+ assertFalse(instructionList.getNth(i).isArgument());
InstructionListIterator iterator = test.listIteratorAt(block, i);
BasicBlock newBlock = iterator.split(code);
- assertTrue(code.isConsistentSSA(appView));
+ assertTrue(code.isConsistentSSAAllowingRedundantBlocks(appView));
assertEquals(initialBlockCount + 1, code.blocks.size());
assertEquals(i + 1, code.entryBlock().getInstructions().size());
@@ -464,11 +465,11 @@
assertEquals(argumentInstructions, test.countArgumentInstructions());
assertEquals(firstBlockInstructions, block.getInstructions().size());
InstructionList instructionList = block.getInstructions();
- assertTrue(!instructionList.getNth(i).isArgument());
+ assertFalse(instructionList.getNth(i).isArgument());
InstructionListIterator iterator = test.listIteratorAt(block, i);
BasicBlock newBlock = iterator.split(code);
- assertTrue(code.isConsistentSSA(appView));
+ assertTrue(code.isConsistentSSAAllowingRedundantBlocks(appView));
assertEquals(initialBlockCount + 1, code.blocks.size());
assertEquals(i + 1, code.entryBlock().getInstructions().size());