/* * Copyright (C) 2015 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 { // Note #1: `javac` flips the conditions of If statements. // Note #2: In the optimizing compiler, the first input of Phi is always // the fall-through path, i.e. the false branch. public static void assertBoolEquals(boolean expected, boolean result) { if (expected != result) { throw new Error("Expected: " + expected + ", found: " + result); } } public static void assertIntEquals(int expected, int result) { if (expected != result) { throw new Error("Expected: " + expected + ", found: " + result); } } /* * Elementary test negating a boolean. Verifies that blocks are merged and * empty branches removed. */ /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (before) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 0 /// CHECK-DAG: <> IntConstant 1 /// CHECK-DAG: If [<>] /// CHECK-DAG: <> Phi [<>,<>] /// CHECK-DAG: Return [<>] /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (before) /// CHECK: Goto /// CHECK: Goto /// CHECK: Goto /// CHECK-NOT: Goto /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 0 /// CHECK-DAG: <> IntConstant 1 /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>] /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (after) /// CHECK-NOT: If /// CHECK-NOT: Phi /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (after) /// CHECK: Goto /// CHECK-NOT: Goto public static boolean BooleanNot(boolean x) { return !x; } /* * Program which only delegates the condition, i.e. returns 1 when True * and 0 when False. */ /// CHECK-START: boolean Main.GreaterThan(int, int) select_generator (before) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 0 /// CHECK-DAG: <> IntConstant 1 /// CHECK-DAG: <> GreaterThan [<>,<>] /// CHECK-DAG: If [<>] /// CHECK-DAG: <> Phi [<>,<>] /// CHECK-DAG: Return [<>] /// CHECK-START: boolean Main.GreaterThan(int, int) select_generator (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 0 /// CHECK-DAG: <> IntConstant 1 /// CHECK-DAG: <> GreaterThan [<>,<>] /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>] public static boolean LessThan(int x, int y) { return (x < y) ? true : false; } /* * Program which further uses negated conditions. * Note that Phis are discovered retrospectively. */ /// CHECK-START: boolean Main.ValuesOrdered(int, int, int) select_generator (before) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 0 /// CHECK-DAG: <> IntConstant 1 /// CHECK-DAG: <> GreaterThan [<>,<>] /// CHECK-DAG: If [<>] /// CHECK-DAG: <> GreaterThan [<>,<>] /// CHECK-DAG: If [<>] /// CHECK-DAG: <> NotEqual [<>,<>] /// CHECK-DAG: If [<>] /// CHECK-DAG: Return [<>] /// CHECK-DAG: <> Phi [<>,<>] /// CHECK-DAG: <> Phi [<>,<>] /// CHECK-DAG: <> Phi [<>,<>] /// CHECK-START: boolean Main.ValuesOrdered(int, int, int) select_generator (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 0 /// CHECK-DAG: <> IntConstant 1 /// CHECK-DAG: <> GreaterThan [<>,<>] /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: <> GreaterThan [<>,<>] /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: <> NotEqual [<>,<>] /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>] public static boolean ValuesOrdered(int x, int y, int z) { return (x <= y) == (y <= z); } /// CHECK-START: int Main.NegatedCondition(boolean) select_generator (before) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 42 /// CHECK-DAG: <> IntConstant 43 /// CHECK-DAG: If [<>] /// CHECK-DAG: <> Phi [<>,<>] /// CHECK-DAG: Return [<>] /// CHECK-START: int Main.NegatedCondition(boolean) select_generator (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 42 /// CHECK-DAG: <> IntConstant 43 /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>] /// CHECK-START: int Main.SimpleTrueBlock(boolean, int) select_generator (after) /// CHECK-NOT: If public static int SimpleTrueBlock(boolean x, int y) { return x ? y + 42 : 43; } /// CHECK-START: int Main.SimpleFalseBlock(boolean, int) select_generator (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 42 /// CHECK-DAG: <> IntConstant 43 /// CHECK-DAG: <> Add [<>,<>] /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>] /// CHECK-START: int Main.SimpleBothBlocks(boolean, int, int) select_generator (after) /// CHECK-NOT: If public static int SimpleBothBlocks(boolean x, int y, int z) { return x ? y + 42 : z + 43; } /// CHECK-START: int Main.ThreeBlocks(boolean, boolean) select_generator (after) /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> ParameterValue /// CHECK-DAG: <> IntConstant 1 /// CHECK-DAG: <> IntConstant 2 /// CHECK-DAG: <> IntConstant 3 /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: <> Select [<>,<>,<>] /// CHECK-DAG: Return [<>] public static int ThreeBlocks(boolean x, boolean y) { if (x) { return 1; } else if (y) { return 2; } else { return 3; } } /// CHECK-START: int Main.MultiplePhis() select_generator (before) /// CHECK-DAG: <> IntConstant 0 /// CHECK-DAG: <> IntConstant 1 /// CHECK-DAG: <> IntConstant 13 /// CHECK-DAG: <> IntConstant 42 /// CHECK-DAG: <> Phi [<>,<>,<>] /// CHECK-DAG: <> Phi [<>,<>,<>] /// CHECK-DAG: <> Add [<>,<>] /// CHECK-DAG: <> LessThanOrEqual [<>,<>] /// CHECK-DAG: If [<>] /// CHECK-DAG: Return [<>] /// CHECK-START: int Main.MultiplePhis() select_generator (after) /// CHECK-DAG: <> IntConstant 0 /// CHECK-DAG: <> IntConstant 1 /// CHECK-DAG: <> IntConstant 13 /// CHECK-DAG: <> IntConstant 42 /// CHECK-DAG: <> Phi [<>,<>] /// CHECK-DAG: <> Phi [<>,<>] /// CHECK-DAG: <> Add [<>,<>] /// CHECK-DAG: <> LessThanOrEqual [<>,<>] /// CHECK-DAG: <