Optimizing: Fix TypeConversion(And(x, const)) simplification.
Avoid introducing implicit conversions when simplifying the
expression TypeConversion(And(x, const)). Previously, when
we dropped the And, we could end up with a TypeConversion to
the same type which should be eliminated on subsequent pass
of the block's instructions; however, a subsequent dependent
TypeConversion in the same block would be processed earlier
and we would unexpectedly see its input as the conversion to
the same type, failing a DCHECK().
Bug: 27626509
Change-Id: I5874a9ceafbf635cf3391beea807ede8468ab5c3
diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc
index 049901b..69dad69 100644
--- a/compiler/optimizing/instruction_simplifier.cc
+++ b/compiler/optimizing/instruction_simplifier.cc
@@ -867,9 +867,7 @@
return;
}
}
- } else if (input->IsAnd() &&
- Primitive::IsIntegralType(result_type) &&
- input->HasOnlyOneNonEnvironmentUse()) {
+ } else if (input->IsAnd() && Primitive::IsIntegralType(result_type)) {
DCHECK(Primitive::IsIntegralType(input_type));
HAnd* input_and = input->AsAnd();
HConstant* constant = input_and->GetConstantRight();
@@ -879,10 +877,18 @@
size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
if (trailing_ones >= kBitsPerByte * Primitive::ComponentSize(result_type)) {
// The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
- input_and->ReplaceWith(input_and->GetLeastConstantLeft());
- input_and->GetBlock()->RemoveInstruction(input_and);
- RecordSimplification();
- return;
+ HInstruction* original_input = input_and->GetLeastConstantLeft();
+ if (IsTypeConversionImplicit(original_input->GetType(), result_type)) {
+ instruction->ReplaceWith(original_input);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ RecordSimplification();
+ return;
+ } else if (input->HasOnlyOneNonEnvironmentUse()) {
+ input_and->ReplaceWith(original_input);
+ input_and->GetBlock()->RemoveInstruction(input_and);
+ RecordSimplification();
+ return;
+ }
}
}
}