diff options
| -rw-r--r-- | compiler/optimizing/graph_checker.cc | 15 | ||||
| -rw-r--r-- | compiler/optimizing/graph_checker.h | 1 | ||||
| -rw-r--r-- | compiler/optimizing/instruction_simplifier.cc | 15 | ||||
| -rw-r--r-- | compiler/optimizing/nodes.h | 2 | ||||
| -rw-r--r-- | test/593-checker-boolean-to-integral-conversions/expected.txt | 1 | ||||
| -rw-r--r-- | test/593-checker-boolean-to-integral-conversions/info.txt | 1 | ||||
| -rw-r--r-- | test/593-checker-boolean-to-integral-conversions/src/Main.java | 232 |
7 files changed, 263 insertions, 4 deletions
diff --git a/compiler/optimizing/graph_checker.cc b/compiler/optimizing/graph_checker.cc index f2e77e28a6..c790d013b5 100644 --- a/compiler/optimizing/graph_checker.cc +++ b/compiler/optimizing/graph_checker.cc @@ -1005,4 +1005,19 @@ void GraphChecker::VisitBoundType(HBoundType* instruction) { } } +void GraphChecker::VisitTypeConversion(HTypeConversion* instruction) { + VisitInstruction(instruction); + Primitive::Type result_type = instruction->GetResultType(); + Primitive::Type input_type = instruction->GetInputType(); + // Invariant: We should never generate a conversion to a Boolean value. + if (result_type == Primitive::kPrimBoolean) { + AddError(StringPrintf( + "%s %d converts to a %s (from a %s).", + instruction->DebugName(), + instruction->GetId(), + Primitive::PrettyDescriptor(result_type), + Primitive::PrettyDescriptor(input_type))); + } +} + } // namespace art diff --git a/compiler/optimizing/graph_checker.h b/compiler/optimizing/graph_checker.h index a536b30fcd..83b198474c 100644 --- a/compiler/optimizing/graph_checker.h +++ b/compiler/optimizing/graph_checker.h @@ -67,6 +67,7 @@ class GraphChecker : public HGraphDelegateVisitor { void VisitReturnVoid(HReturnVoid* ret) OVERRIDE; void VisitSelect(HSelect* instruction) OVERRIDE; void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE; + void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE; void HandleLoop(HBasicBlock* loop_header); void HandleBooleanInput(HInstruction* instruction, size_t input_index); diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc index 1249b48e1e..1f66db7909 100644 --- a/compiler/optimizing/instruction_simplifier.cc +++ b/compiler/optimizing/instruction_simplifier.cc @@ -786,14 +786,21 @@ void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { } static bool IsTypeConversionImplicit(Primitive::Type input_type, Primitive::Type result_type) { + // Invariant: We should never generate a conversion to a Boolean value. + DCHECK_NE(Primitive::kPrimBoolean, result_type); + // Besides conversion to the same type, widening integral conversions are implicit, // excluding conversions to long and the byte->char conversion where we need to // clear the high 16 bits of the 32-bit sign-extended representation of byte. return result_type == input_type || - (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimByte) || - (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimShort) || - (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimChar) || - (result_type == Primitive::kPrimShort && input_type == Primitive::kPrimByte); + (result_type == Primitive::kPrimInt && (input_type == Primitive::kPrimBoolean || + input_type == Primitive::kPrimByte || + input_type == Primitive::kPrimShort || + input_type == Primitive::kPrimChar)) || + (result_type == Primitive::kPrimChar && input_type == Primitive::kPrimBoolean) || + (result_type == Primitive::kPrimShort && (input_type == Primitive::kPrimBoolean || + input_type == Primitive::kPrimByte)) || + (result_type == Primitive::kPrimByte && input_type == Primitive::kPrimBoolean); } static bool IsTypeConversionLossless(Primitive::Type input_type, Primitive::Type result_type) { diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h index ba4242112b..9425ef3267 100644 --- a/compiler/optimizing/nodes.h +++ b/compiler/optimizing/nodes.h @@ -4904,6 +4904,8 @@ class HTypeConversion : public HExpression<1> { dex_pc) { SetRawInputAt(0, input); DCHECK_NE(input->GetType(), result_type); + // Invariant: We should never generate a conversion to a Boolean value. + DCHECK_NE(Primitive::kPrimBoolean, result_type); } HInstruction* GetInput() const { return InputAt(0); } diff --git a/test/593-checker-boolean-to-integral-conversions/expected.txt b/test/593-checker-boolean-to-integral-conversions/expected.txt new file mode 100644 index 0000000000..b0aad4deb5 --- /dev/null +++ b/test/593-checker-boolean-to-integral-conversions/expected.txt @@ -0,0 +1 @@ +passed diff --git a/test/593-checker-boolean-to-integral-conversions/info.txt b/test/593-checker-boolean-to-integral-conversions/info.txt new file mode 100644 index 0000000000..2d883c77ee --- /dev/null +++ b/test/593-checker-boolean-to-integral-conversions/info.txt @@ -0,0 +1 @@ +Regression test for Boolean to integral types conversions. diff --git a/test/593-checker-boolean-to-integral-conversions/src/Main.java b/test/593-checker-boolean-to-integral-conversions/src/Main.java new file mode 100644 index 0000000000..ba65839376 --- /dev/null +++ b/test/593-checker-boolean-to-integral-conversions/src/Main.java @@ -0,0 +1,232 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +public class Main { + + public static void main(String args[]) { + expectEqualsByte((byte)1, booleanToByte(true)); + expectEqualsShort((short)1, booleanToShort(true)); + expectEqualsChar((char)1, booleanToChar(true)); + expectEqualsInt(1, booleanToInt(true)); + expectEqualsLong(1L, booleanToLong(true)); + + expectEqualsInt(1, longToIntOfBoolean()); + + System.out.println("passed"); + } + + /// CHECK-START: byte Main.booleanToByte(boolean) builder (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Cond:z\d+>> Equal [<<Arg>>,<<Zero>>] + /// CHECK-DAG: If [<<Cond>>] + /// CHECK-DAG: <<Phi:i\d+>> Phi [<<One>>,<<Zero>>] + /// CHECK-DAG: <<IToS:b\d+>> TypeConversion [<<Phi>>] + /// CHECK-DAG: Return [<<IToS>>] + + /// CHECK-START: byte Main.booleanToByte(boolean) select_generator (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Sel:i\d+>> Select [<<Zero>>,<<One>>,<<Arg>>] + /// CHECK-DAG: <<IToS:b\d+>> TypeConversion [<<Sel>>] + /// CHECK-DAG: Return [<<IToS>>] + + /// CHECK-START: byte Main.booleanToByte(boolean) instruction_simplifier_after_bce (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: Return [<<Arg>>] + + static byte booleanToByte(boolean b) { + return (byte)(b ? 1 : 0); + } + + /// CHECK-START: short Main.booleanToShort(boolean) builder (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Cond:z\d+>> Equal [<<Arg>>,<<Zero>>] + /// CHECK-DAG: If [<<Cond>>] + /// CHECK-DAG: <<Phi:i\d+>> Phi [<<One>>,<<Zero>>] + /// CHECK-DAG: <<IToS:s\d+>> TypeConversion [<<Phi>>] + /// CHECK-DAG: Return [<<IToS>>] + + /// CHECK-START: short Main.booleanToShort(boolean) select_generator (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Sel:i\d+>> Select [<<Zero>>,<<One>>,<<Arg>>] + /// CHECK-DAG: <<IToS:s\d+>> TypeConversion [<<Sel>>] + /// CHECK-DAG: Return [<<IToS>>] + + /// CHECK-START: short Main.booleanToShort(boolean) instruction_simplifier_after_bce (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: Return [<<Arg>>] + + static short booleanToShort(boolean b) { + return (short)(b ? 1 : 0); + } + + /// CHECK-START: char Main.booleanToChar(boolean) builder (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Cond:z\d+>> Equal [<<Arg>>,<<Zero>>] + /// CHECK-DAG: If [<<Cond>>] + /// CHECK-DAG: <<Phi:i\d+>> Phi [<<One>>,<<Zero>>] + /// CHECK-DAG: <<IToC:c\d+>> TypeConversion [<<Phi>>] + /// CHECK-DAG: Return [<<IToC>>] + + /// CHECK-START: char Main.booleanToChar(boolean) select_generator (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Sel:i\d+>> Select [<<Zero>>,<<One>>,<<Arg>>] + /// CHECK-DAG: <<IToC:c\d+>> TypeConversion [<<Sel>>] + /// CHECK-DAG: Return [<<IToC>>] + + /// CHECK-START: char Main.booleanToChar(boolean) instruction_simplifier_after_bce (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: Return [<<Arg>>] + + static char booleanToChar(boolean b) { + return (char)(b ? 1 : 0); + } + + /// CHECK-START: int Main.booleanToInt(boolean) builder (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Cond:z\d+>> Equal [<<Arg>>,<<Zero>>] + /// CHECK-DAG: If [<<Cond>>] + /// CHECK-DAG: <<Phi:i\d+>> Phi [<<One>>,<<Zero>>] + /// CHECK-DAG: Return [<<Phi>>] + + /// CHECK-START: int Main.booleanToInt(boolean) select_generator (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Sel:i\d+>> Select [<<Zero>>,<<One>>,<<Arg>>] + /// CHECK-DAG: Return [<<Sel>>] + + /// CHECK-START: int Main.booleanToInt(boolean) instruction_simplifier_after_bce (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: Return [<<Arg>>] + + static int booleanToInt(boolean b) { + return b ? 1 : 0; + } + + /// CHECK-START: long Main.booleanToLong(boolean) builder (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Cond:z\d+>> Equal [<<Arg>>,<<Zero>>] + /// CHECK-DAG: If [<<Cond>>] + /// CHECK-DAG: <<Phi:i\d+>> Phi [<<One>>,<<Zero>>] + /// CHECK-DAG: <<IToJ:j\d+>> TypeConversion [<<Phi>>] + /// CHECK-DAG: Return [<<IToJ>>] + + /// CHECK-START: long Main.booleanToLong(boolean) select_generator (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Sel:i\d+>> Select [<<Zero>>,<<One>>,<<Arg>>] + /// CHECK-DAG: <<IToJ:j\d+>> TypeConversion [<<Sel>>] + /// CHECK-DAG: Return [<<IToJ>>] + + /// CHECK-START: long Main.booleanToLong(boolean) instruction_simplifier_after_bce (after) + /// CHECK: <<Arg:z\d+>> ParameterValue + /// CHECK-DAG: <<ZToJ:j\d+>> TypeConversion [<<Arg>>] + /// CHECK-DAG: Return [<<ZToJ>>] + + static long booleanToLong(boolean b) { + return b ? 1 : 0; + } + + /// CHECK-START: int Main.longToIntOfBoolean() builder (after) + /// CHECK-DAG: <<Method:[ij]\d+>> CurrentMethod + /// CHECK-DAG: <<Sget:z\d+>> StaticFieldGet + /// CHECK-DAG: <<ZToJ:j\d+>> InvokeStaticOrDirect [<<Sget>>,<<Method>>] + /// CHECK-DAG: <<JToI:i\d+>> TypeConversion [<<ZToJ>>] + /// CHECK-DAG: Return [<<JToI>>] + + /// CHECK-START: int Main.longToIntOfBoolean() inliner (after) + /// CHECK-DAG: <<Method:[ij]\d+>> CurrentMethod + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Sget:z\d+>> StaticFieldGet + /// CHECK-DAG: If [<<Sget>>] + /// CHECK-DAG: <<Phi:i\d+>> Phi [<<One>>,<<Zero>>] + /// CHECK-DAG: <<IToJ:j\d+>> TypeConversion [<<Phi>>] + /// CHECK-DAG: <<JToI:i\d+>> TypeConversion [<<IToJ>>] + /// CHECK-DAG: Return [<<JToI>>] + + /// CHECK-START: int Main.longToIntOfBoolean() select_generator (after) + /// CHECK-DAG: <<Method:[ij]\d+>> CurrentMethod + /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 + /// CHECK-DAG: <<One:i\d+>> IntConstant 1 + /// CHECK-DAG: <<Sget:z\d+>> StaticFieldGet + /// CHECK-DAG: <<Sel:i\d+>> Select [<<Zero>>,<<One>>,<<Sget>>] + /// CHECK-DAG: <<IToJ:j\d+>> TypeConversion [<<Sel>>] + /// CHECK-DAG: <<JToI:i\d+>> TypeConversion [<<IToJ>>] + /// CHECK-DAG: Return [<<JToI>>] + + /// CHECK-START: int Main.longToIntOfBoolean() instruction_simplifier_after_bce (after) + /// CHECK-DAG: <<Method:[ij]\d+>> CurrentMethod + /// CHECK-DAG: <<Sget:z\d+>> StaticFieldGet + /// CHECK-DAG: Return [<<Sget>>] + + static int longToIntOfBoolean() { + long l = booleanToLong(booleanField); + return (int) l; + } + + + private static void expectEqualsByte(byte expected, byte result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + private static void expectEqualsShort(short expected, short result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + private static void expectEqualsChar(char expected, char result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + private static void expectEqualsInt(int expected, int result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + private static void expectEqualsLong(long expected, long result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } + + + public static boolean booleanField = true; + +} |