type conversion elimination for constant input
type conversion on constant input can be eliminated if the constant
value falls in the result type's range.
Test: run-test on host, 711-checker-type-conversion
Change-Id: I372139d681aa06fa6e760d7814c86ac949292813
diff --git a/compiler/optimizing/data_type.h b/compiler/optimizing/data_type.h
index 75a7fbe..d253036 100644
--- a/compiler/optimizing/data_type.h
+++ b/compiler/optimizing/data_type.h
@@ -186,6 +186,7 @@
}
static bool IsTypeConversionImplicit(Type input_type, Type result_type);
+ static bool IsTypeConversionImplicit(int64_t value, Type result_type);
static const char* PrettyDescriptor(Type type);
@@ -213,6 +214,18 @@
MaxValueOfIntegralType(input_type) <= MaxValueOfIntegralType(result_type));
}
+inline bool DataType::IsTypeConversionImplicit(int64_t value, Type result_type) {
+ if (IsIntegralType(result_type) && result_type != Type::kInt64) {
+ // If the constant value falls in the range of the result_type, type
+ // conversion isn't needed.
+ return value >= MinValueOfIntegralType(result_type) &&
+ value <= MaxValueOfIntegralType(result_type);
+ }
+ // Conversion isn't implicit if it's into non-integer types, or 64-bit int
+ // which may have different number of registers.
+ return false;
+}
+
} // namespace art
#endif // ART_COMPILER_OPTIMIZING_DATA_TYPE_H_
diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc
index fbfee12..a6dfa47 100644
--- a/compiler/optimizing/instruction_simplifier.cc
+++ b/compiler/optimizing/instruction_simplifier.cc
@@ -1159,6 +1159,16 @@
RecordSimplification();
return;
}
+ } else if (input->IsIntConstant()) {
+ // Try to eliminate type conversion on int constant whose value falls into
+ // the range of the result type.
+ int32_t value = input->AsIntConstant()->GetValue();
+ if (DataType::IsTypeConversionImplicit(value, result_type)) {
+ instruction->ReplaceWith(input);
+ instruction->GetBlock()->RemoveInstruction(instruction);
+ RecordSimplification();
+ return;
+ }
}
}