Type conversion elimination of constants
A better way of eliminating type conversion for constants.
Test: run-test on host. 711-checker-type-conversion.
Change-Id: I457bc091542a5ac4cc4e77cadb012ee7cb040ce8
diff --git a/compiler/optimizing/constant_folding.cc b/compiler/optimizing/constant_folding.cc
index bb586bf..6f11e62 100644
--- a/compiler/optimizing/constant_folding.cc
+++ b/compiler/optimizing/constant_folding.cc
@@ -113,7 +113,7 @@
void HConstantFoldingVisitor::VisitTypeConversion(HTypeConversion* inst) {
// Constant folding: replace `TypeConversion(a)' with a constant at
// compile time if `a' is a constant.
- HConstant* constant = inst->AsTypeConversion()->TryStaticEvaluation();
+ HConstant* constant = inst->TryStaticEvaluation();
if (constant != nullptr) {
inst->ReplaceWith(constant);
inst->GetBlock()->RemoveInstruction(inst);
diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc
index bd20d28..7fa0c2b 100644
--- a/compiler/optimizing/instruction_simplifier.cc
+++ b/compiler/optimizing/instruction_simplifier.cc
@@ -1168,16 +1168,6 @@
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;
- }
}
}
diff --git a/compiler/optimizing/nodes.cc b/compiler/optimizing/nodes.cc
index fa580d9..4a9da7e 100644
--- a/compiler/optimizing/nodes.cc
+++ b/compiler/optimizing/nodes.cc
@@ -1403,6 +1403,14 @@
if (GetInput()->IsIntConstant()) {
int32_t value = GetInput()->AsIntConstant()->GetValue();
switch (GetResultType()) {
+ case DataType::Type::kInt8:
+ return graph->GetIntConstant(static_cast<int8_t>(value), GetDexPc());
+ case DataType::Type::kUint8:
+ return graph->GetIntConstant(static_cast<uint8_t>(value), GetDexPc());
+ case DataType::Type::kInt16:
+ return graph->GetIntConstant(static_cast<int16_t>(value), GetDexPc());
+ case DataType::Type::kUint16:
+ return graph->GetIntConstant(static_cast<uint16_t>(value), GetDexPc());
case DataType::Type::kInt64:
return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc());
case DataType::Type::kFloat32:
@@ -1415,6 +1423,14 @@
} else if (GetInput()->IsLongConstant()) {
int64_t value = GetInput()->AsLongConstant()->GetValue();
switch (GetResultType()) {
+ case DataType::Type::kInt8:
+ return graph->GetIntConstant(static_cast<int8_t>(value), GetDexPc());
+ case DataType::Type::kUint8:
+ return graph->GetIntConstant(static_cast<uint8_t>(value), GetDexPc());
+ case DataType::Type::kInt16:
+ return graph->GetIntConstant(static_cast<int16_t>(value), GetDexPc());
+ case DataType::Type::kUint16:
+ return graph->GetIntConstant(static_cast<uint16_t>(value), GetDexPc());
case DataType::Type::kInt32:
return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc());
case DataType::Type::kFloat32:
diff --git a/test/711-checker-type-conversion/src/Main.java b/test/711-checker-type-conversion/src/Main.java
index 64ffcd2..2c9c3a1 100644
--- a/test/711-checker-type-conversion/src/Main.java
+++ b/test/711-checker-type-conversion/src/Main.java
@@ -22,20 +22,15 @@
}
}
- /// CHECK-START: byte Main.getByte1() instruction_simplifier (before)
+ /// CHECK-START: byte Main.getByte1() constant_folding (before)
/// CHECK: TypeConversion
/// CHECK: TypeConversion
/// CHECK: Add
/// CHECK: TypeConversion
- /// CHECK-START: byte Main.getByte1() instruction_simplifier (after)
+ /// CHECK-START: byte Main.getByte1() constant_folding (after)
/// CHECK-NOT: TypeConversion
- /// CHECK: Add
- /// CHECK: TypeConversion
-
- /// CHECK-START: byte Main.getByte1() instruction_simplifier$before_codegen (after)
/// CHECK-NOT: Add
- /// CHECK-NOT: TypeConversion
static byte getByte1() {
int i = -2;
@@ -43,20 +38,15 @@
return (byte)((byte)i + (byte)j);
}
- /// CHECK-START: byte Main.getByte2() instruction_simplifier (before)
+ /// CHECK-START: byte Main.getByte2() constant_folding (before)
/// CHECK: TypeConversion
/// CHECK: TypeConversion
/// CHECK: Add
/// CHECK: TypeConversion
- /// CHECK-START: byte Main.getByte2() instruction_simplifier (after)
+ /// CHECK-START: byte Main.getByte2() constant_folding (after)
/// CHECK-NOT: TypeConversion
- /// CHECK: Add
- /// CHECK: TypeConversion
-
- /// CHECK-START: byte Main.getByte2() instruction_simplifier$before_codegen (after)
/// CHECK-NOT: Add
- /// CHECK: TypeConversion
static byte getByte2() {
int i = -100;
@@ -64,8 +54,24 @@
return (byte)((byte)i + (byte)j);
}
+ /// CHECK-START: byte Main.getByte3() constant_folding (before)
+ /// CHECK: TypeConversion
+ /// CHECK: TypeConversion
+ /// CHECK: Add
+ /// CHECK: TypeConversion
+
+ /// CHECK-START: byte Main.getByte2() constant_folding (after)
+ /// CHECK-NOT: TypeConversion
+ /// CHECK-NOT: Add
+
+ static byte getByte3() {
+ long i = 0xabcdabcdabcdL;
+ return (byte)((byte)i + (byte)i);
+ }
+
public static void main(String[] args) {
assertByteEquals(getByte1(), (byte)-5);
assertByteEquals(getByte2(), (byte)(-201));
+ assertByteEquals(getByte3(), (byte)(0xcd + 0xcd));
}
}