Account for assume instructions from if-instance-of in if optimizer
This fixes an issue where code on the form:
if (o instanceof T) {
T t = (T) o;
} else {
}
... were incorrectly optimized to:
o instanceof T;
T t = (T) o;
This was due to the if optimizer incorrectly concluding that the else-branch always has the same behavior as the then-branch, since the cast to T is guaranteed to succeed in the presence of the assume instruction that has been synthesized for the `o instanceof T` branch.
Bug: b/401515589
Change-Id: Ibac1c72dcba660e4f54bca6b0bdea753482c0a11
diff --git a/src/main/java/com/android/tools/r8/ir/analysis/equivalence/BasicBlockBehavioralSubsumption.java b/src/main/java/com/android/tools/r8/ir/analysis/equivalence/BasicBlockBehavioralSubsumption.java
index 8d2d7fc..d93664a 100644
--- a/src/main/java/com/android/tools/r8/ir/analysis/equivalence/BasicBlockBehavioralSubsumption.java
+++ b/src/main/java/com/android/tools/r8/ir/analysis/equivalence/BasicBlockBehavioralSubsumption.java
@@ -66,10 +66,15 @@
}
}
if (assumptionUses != null) {
+ Value indirectConditionValue =
+ conditionValue.isDefinedByInstructionSatisfying(Instruction::isInstanceOf)
+ ? conditionValue.getDefinition().asInstanceOf().value()
+ : null;
while (assumptionUses.hasNext()) {
Assume next = assumptionUses.next();
for (Value assumptionArgument : next.inValues()) {
- if (assumptionArgument == conditionValue) {
+ if (assumptionArgument == conditionValue
+ || assumptionArgument == indirectConditionValue) {
return true;
}
Assume indirectAssumption =