Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2014 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 
     19   public static void doNothing(boolean b) {
     20     System.out.println("In do nothing.");
     21   }
     22 
     23   public static void inIf() {
     24     System.out.println("In if.");
     25   }
     26 
     27   public static int bar() {
     28     return 42;
     29   }
     30 
     31   public static int foo1() {
     32     int b = bar();
     33     doNothing(b == 42);
     34     // This second `b == 42` will be GVN'ed away.
     35     if (b == 42) {
     36       inIf();
     37       return b;
     38     }
     39     return 0;
     40   }
     41 
     42   public static int foo2() {
     43     int b = bar();
     44     doNothing(b == 41);
     45     // This second `b == 41` will be GVN'ed away.
     46     if (b == 41) {
     47       inIf();
     48       return 0;
     49     }
     50     return b;
     51   }
     52 
     53   public static boolean $noinline$intEq0(int x) {
     54     return x == 0;
     55   }
     56 
     57   public static boolean $noinline$intNe0(int x) {
     58     return x != 0;
     59   }
     60 
     61   public static boolean $noinline$longEq0(long x) {
     62     return x == 0;
     63   }
     64 
     65   public static boolean $noinline$longNe0(long x) {
     66     return x != 0;
     67   }
     68 
     69   public static boolean $noinline$longEqCst(long x) {
     70     return x == 0x0123456789ABCDEFL;
     71   }
     72 
     73   public static boolean $noinline$longNeCst(long x) {
     74     return x != 0x0123456789ABCDEFL;
     75   }
     76 
     77   public static void assertEqual(boolean expected, boolean actual) {
     78     if (expected != actual) {
     79       throw new Error("Assertion failed: " + expected + " != " + actual);
     80     }
     81   }
     82 
     83   // The purpose of this method is to test code generation for a materialized
     84   // HCondition that is not equality or inequality, and that has one boolean
     85   // input. That can't be done directly, so we have to rely on the instruction
     86   // simplifier to transform the control-flow graph appropriately.
     87   public static boolean $noinline$booleanCondition(boolean in) {
     88     int value = in ? 1 : 0;
     89 
     90     // Calling a non-inlineable method that uses `value` as well prevents a
     91     // transformation of the return value into `false`.
     92     $noinline$intNe0(value);
     93     return value > 127;
     94   }
     95 
     96   public static void main(String[] args) {
     97     System.out.println("foo1");
     98     int res = foo1();
     99     if (res != 42) {
    100       throw new Error("Unexpected return value for foo1: " + res + ", expected 42.");
    101     }
    102 
    103     System.out.println("foo2");
    104     res = foo2();
    105     if (res != 42) {
    106       throw new Error("Unexpected return value for foo2: " + res + ", expected 42.");
    107     }
    108 
    109     assertEqual($noinline$booleanCondition(false), false);
    110     assertEqual($noinline$booleanCondition(true), false);
    111 
    112     int[] int_inputs = {0, 1, -1, Integer.MIN_VALUE, Integer.MAX_VALUE, 42, -9000};
    113     long[] long_inputs = {
    114         0L, 1L, -1L, Long.MIN_VALUE, Long.MAX_VALUE, 0x100000000L,
    115         0x100000001L, -9000L, 0x0123456789ABCDEFL};
    116 
    117     boolean[] int_eq_0_expected = {true, false, false, false, false, false, false};
    118 
    119     for (int i = 0; i < int_inputs.length; i++) {
    120       assertEqual(int_eq_0_expected[i], $noinline$intEq0(int_inputs[i]));
    121     }
    122 
    123     boolean[] int_ne_0_expected = {false, true, true, true, true, true, true};
    124 
    125     for (int i = 0; i < int_inputs.length; i++) {
    126       assertEqual(int_ne_0_expected[i], $noinline$intNe0(int_inputs[i]));
    127     }
    128 
    129     boolean[] long_eq_0_expected = {true, false, false, false, false, false, false, false, false};
    130 
    131     for (int i = 0; i < long_inputs.length; i++) {
    132       assertEqual(long_eq_0_expected[i], $noinline$longEq0(long_inputs[i]));
    133     }
    134 
    135     boolean[] long_ne_0_expected = {false, true, true, true, true, true, true, true, true};
    136 
    137     for (int i = 0; i < long_inputs.length; i++) {
    138       assertEqual(long_ne_0_expected[i], $noinline$longNe0(long_inputs[i]));
    139     }
    140 
    141     boolean[] long_eq_cst_expected = {false, false, false, false, false, false, false, false, true};
    142 
    143     for (int i = 0; i < long_inputs.length; i++) {
    144       assertEqual(long_eq_cst_expected[i], $noinline$longEqCst(long_inputs[i]));
    145     }
    146 
    147     boolean[] long_ne_cst_expected = {true, true, true, true, true, true, true, true, false};
    148 
    149     for (int i = 0; i < long_inputs.length; i++) {
    150       assertEqual(long_ne_cst_expected[i], $noinline$longNeCst(long_inputs[i]));
    151     }
    152   }
    153 }
    154