/* * Copyright (C) 2017 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 { // Check that we don't generate a select since we don't have a Phi (not even at // the builder stage) since both values are the same. /// CHECK-START: int Main.$noinline$testSimpleDiamondSameValue(boolean) builder (after) /// CHECK-NOT: Phi /// CHECK-START: int Main.$noinline$testSimpleDiamondSameValue(boolean) control_flow_simplifier (before) /// CHECK-NOT: Phi /// CHECK-START: int Main.$noinline$testSimpleDiamondSameValue(boolean) control_flow_simplifier (after) /// CHECK-NOT: Phi /// CHECK-START: int Main.$noinline$testSimpleDiamondSameValue(boolean) control_flow_simplifier (after) /// CHECK-NOT: Select private static int $noinline$testSimpleDiamondSameValue(boolean bool_param) { int return_value; if (bool_param) { return_value = 10; } else { return_value = 10; } return return_value; } // Check that we generate a select for a simple diamond pattern, with different values. /// CHECK-START: int Main.$noinline$testSimpleDiamondDifferentValue(boolean) control_flow_simplifier (before) /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: <> Phi [<>,<>] /// CHECK-DAG: Return [<>] /// CHECK-EVAL: set(["<>","<>"]) == set(["<>","<>"]) /// CHECK-START: int Main.$noinline$testSimpleDiamondDifferentValue(boolean) control_flow_simplifier (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>] private static int $noinline$testDoubleDiamondSameValueButNotAllOuter(boolean bool_param_1, boolean bool_param_2) { int return_value; if (bool_param_1) { return_value = 10; } else { if (bool_param_2) { return_value = 20; } else { return_value = 20; } } return return_value; } // Check that we generate a select for a double diamond pattern, with a different value in the inner branch. /// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllInner(boolean, boolean) control_flow_simplifier (before) /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: <> Phi [<>,<>,<>] /// CHECK-DAG: Return [<>] /// CHECK-EVAL: set(["<>","<>","<>"]) == set(["<>","<>","<>"]) /// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllInner(boolean, boolean) control_flow_simplifier (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>] private static int $noinline$testDoubleDiamondDifferentValue(boolean bool_param_1, boolean bool_param_2) { int return_value; if (bool_param_1) { return_value = 10; } else { if (bool_param_2) { return_value = 20; } else { return_value = 30; } } return return_value; } private static void assertEquals(int expected, int actual) { if (expected != actual) { throw new AssertionError("Expected " + expected + " got " + actual); } } // Check that we generate a select, which we collapse into a single return. /// CHECK-START: int Main.$noinline$testSimpleDiamondSameValueWithReturn(boolean) builder (after) /// CHECK: <> IntConstant 10 /// CHECK: Return [<>] /// CHECK: Return [<>] /// CHECK-START: int Main.$noinline$testSimpleDiamondSameValueWithReturn(boolean) control_flow_simplifier (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-START: int Main.$noinline$testSimpleDiamondSameValueWithReturn(boolean) instruction_simplifier$after_gvn (after) /// CHECK: <> IntConstant 10 /// CHECK: Return [<>] /// CHECK-START: int Main.$noinline$testSimpleDiamondSameValueWithReturn(boolean) instruction_simplifier$after_gvn (after) /// CHECK: Return /// CHECK-NOT: Return private static int $noinline$testSimpleDiamondSameValueWithReturn(boolean bool_param) { if (bool_param) { return 10; } else { return 10; } } // Same as testSimpleDiamondDifferentValue, but branches return. /// CHECK-START: int Main.$noinline$testSimpleDiamondDifferentValueWithReturn(boolean) control_flow_simplifier (before) /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: Return [<>] /// CHECK-DAG: Return [<>] /// CHECK-START: int Main.$noinline$testSimpleDiamondDifferentValueWithReturn(boolean) control_flow_simplifier (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>,<>,<>] /// CHECK-DAG: Return [<>] /// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueWithReturn(boolean, boolean) instruction_simplifier$after_gvn (after) /// CHECK: <> IntConstant 10 /// CHECK: Return [<>] /// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueWithReturn(boolean, boolean) instruction_simplifier$after_gvn (after) /// CHECK: Return /// CHECK-NOT: Return private static int $noinline$testDoubleDiamondSameValueWithReturn(boolean bool_param_1, boolean bool_param_2) { if (bool_param_1) { return 10; } else { if (bool_param_2) { return 10; } else { return 10; } } } // Same as testDoubleDiamondSameValueButNotAllOuter, but branches return. /// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllOuterWithReturn(boolean, boolean) control_flow_simplifier (before) /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: Return [<>] /// CHECK-DAG: Return [<>] /// CHECK-DAG: Return [<>] // Note that we have 3 returns as D8 only merges when the line positions are equal. /// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllOuterWithReturn(boolean, boolean) control_flow_simplifier (before) /// CHECK: Return /// CHECK: Return /// CHECK: Return /// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllOuterWithReturn(boolean, boolean) control_flow_simplifier (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>] private static int $noinline$testDoubleDiamondSameValueButNotAllInnerWithReturn(boolean bool_param_1, boolean bool_param_2) { if (bool_param_1) { return 20; } else { if (bool_param_2) { return 10; } else { return 20; } } } // Same as testDoubleDiamondDifferentValue, but branches return. /// CHECK-START: int Main.$noinline$testDoubleDiamondDifferentValueWithReturn(boolean, boolean) control_flow_simplifier (before) /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: <> IntConstant 30 /// CHECK-DAG: Return [<>] /// CHECK-DAG: Return [<>] /// CHECK-DAG: Return [<>] /// CHECK-START: int Main.$noinline$testDoubleDiamondDifferentValueWithReturn(boolean, boolean) control_flow_simplifier (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 10 /// CHECK-DAG: <> IntConstant 20 /// CHECK-DAG: <> IntConstant 30 /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: <> Select [<