1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 public class Main { 18 main(String args[])19 public static void main(String args[]) { 20 doFloatingPointTest("1", "1.0"); 21 doFloatingPointTest("4", "2.0"); 22 checkValue(String.valueOf(doIntegerTest1(4)), "0"); 23 checkValue(String.valueOf(doIntegerTest2(4)), "4"); 24 25 // Another variant of the floating point test, but less brittle. 26 staticField = 1; 27 checkValue(String.valueOf($noinline$test()), "1.0"); 28 staticField = 4; 29 checkValue(String.valueOf($noinline$test()), "2.0"); 30 } 31 32 // This code is a reduced version of the original reproducer. The arm 33 // code generator used to generate wrong code for it. Note that this 34 // test is very brittle and a simple change in it could cause the compiler 35 // to not trip. doFloatingPointTest(String s, String expected)36 public static void doFloatingPointTest(String s, String expected) { 37 float a = (float)Integer.valueOf(s); 38 a = a < 2.0f ? a : 2.0f; 39 checkValue("" + a, expected); 40 } 41 42 // The compiler used to trip on the two following methods. The test there 43 // is very brittle and requires not running constant folding after 44 // load/store elimination. 45 public static int doIntegerTest1(int param) { 46 Main main = new Main(); 47 main.field = 0; 48 return (main.field == 0) ? 0 : param; 49 } 50 51 public static int doIntegerTest2(int param) { 52 Main main = new Main(); 53 main.field = 0; 54 return (main.field != 0) ? 0 : param; 55 } 56 57 public static void checkValue(String actual, String expected) { 58 if (!expected.equals(actual)) { 59 throw new Error("Expected " + expected + ", got " + actual); 60 } 61 } 62 63 static void $noinline$nothing() {} 64 static int $noinline$getField() { return staticField; } 65 66 static float $noinline$test() { 67 // The 2.0f shall be materialized for GreaterThanOrEqual at the beginning of the method; 68 // since the following call clobbers caller-saves, it is allocated to s16. 69 // r0(field) = InvokeStaticOrDirect[] 70 int one = $noinline$getField(); 71 // s0(a_1) = TypeConversion[r0(one)] 72 float a = (float)one; 73 // s16(a_2) = Select[s0(a_1), C(2.0f), GreaterThanOrEqual[s0(a_1), s16(2.0f)]] 74 a = a < 2.0f ? a : 2.0f; 75 // The following call is added to clobber caller-saves, forcing the output of the Select 76 // to be allocated to s16. 77 $noinline$nothing(); 78 return a; 79 } 80 81 int field; 82 static int staticField; 83 } 84