Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2015 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 // TODO: Add more tests after we can inline functions with calls.
     18 
     19 class ClassWithoutFinals {
     20   /// CHECK-START: void ClassWithoutFinals.<init>() register (after)
     21   /// CHECK-NOT: MemoryBarrier kind:StoreStore
     22   public ClassWithoutFinals() {}
     23 }
     24 
     25 class ClassWithFinals {
     26   public final int x;
     27   public ClassWithFinals obj;
     28   public static boolean doThrow = false;
     29 
     30   /// CHECK-START: void ClassWithFinals.<init>(boolean) register (after)
     31   /// CHECK:      MemoryBarrier kind:StoreStore
     32   /// CHECK-NEXT: ReturnVoid
     33   public ClassWithFinals(boolean cond) {
     34     x = 0;
     35     if (doThrow) {
     36       // avoid inlining
     37       throw new RuntimeException();
     38     }
     39   }
     40 
     41   /// CHECK-START: void ClassWithFinals.<init>() register (after)
     42   /// CHECK:      MemoryBarrier kind:StoreStore
     43   /// CHECK-NEXT: ReturnVoid
     44   public ClassWithFinals() {
     45     x = 0;
     46   }
     47 
     48   /// CHECK-START: void ClassWithFinals.<init>(int) register (after)
     49   /// CHECK:      MemoryBarrier kind:StoreStore
     50   /// CHECK:      MemoryBarrier kind:StoreStore
     51   /// CHECK-NEXT: ReturnVoid
     52   public ClassWithFinals(int x) {
     53     // This should have two barriers:
     54     //   - one for the constructor
     55     //   - one for the `new` which should be inlined.
     56     obj = new ClassWithFinals();
     57     this.x = x;
     58   }
     59 }
     60 
     61 class InheritFromClassWithFinals extends ClassWithFinals {
     62   /// CHECK-START: void InheritFromClassWithFinals.<init>() register (after)
     63   /// CHECK:      MemoryBarrier kind:StoreStore
     64   /// CHECK-NEXT: ReturnVoid
     65 
     66   /// CHECK-START: void InheritFromClassWithFinals.<init>() register (after)
     67   /// CHECK-NOT:  InvokeStaticOrDirect
     68   public InheritFromClassWithFinals() {
     69     // Should inline the super constructor.
     70   }
     71 
     72   /// CHECK-START: void InheritFromClassWithFinals.<init>(boolean) register (after)
     73   /// CHECK:      InvokeStaticOrDirect
     74 
     75   /// CHECK-START: void InheritFromClassWithFinals.<init>(boolean) register (after)
     76   /// CHECK-NOT:  MemoryBarrier kind:StoreStore
     77   public InheritFromClassWithFinals(boolean cond) {
     78     super(cond);
     79     // should not inline the super constructor
     80   }
     81 
     82   /// CHECK-START: void InheritFromClassWithFinals.<init>(int) register (after)
     83   /// CHECK:      MemoryBarrier kind:StoreStore
     84   /// CHECK:      MemoryBarrier kind:StoreStore
     85   /// CHECK:      ReturnVoid
     86 
     87   /// CHECK-START: void InheritFromClassWithFinals.<init>(int) register (after)
     88   /// CHECK-NOT:  InvokeStaticOrDirect
     89   public InheritFromClassWithFinals(int unused) {
     90     // Should inline the super constructor and insert a memory barrier.
     91 
     92     // Should inline the new instance call and insert another memory barrier.
     93     new InheritFromClassWithFinals();
     94   }
     95 }
     96 
     97 class HaveFinalsAndInheritFromClassWithFinals extends ClassWithFinals {
     98   final int y;
     99 
    100   /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>() register (after)
    101   /// CHECK:      MemoryBarrier kind:StoreStore
    102   /// CHECK-NEXT: ReturnVoid
    103 
    104   /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>() register (after)
    105   /// CHECK-NOT: InvokeStaticOrDirect
    106   public HaveFinalsAndInheritFromClassWithFinals() {
    107     // Should inline the super constructor and remove the memory barrier.
    108     y = 0;
    109   }
    110 
    111   /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>(boolean) register (after)
    112   /// CHECK:      InvokeStaticOrDirect
    113   /// CHECK:      MemoryBarrier kind:StoreStore
    114   /// CHECK-NEXT: ReturnVoid
    115   public HaveFinalsAndInheritFromClassWithFinals(boolean cond) {
    116     super(cond);
    117     // should not inline the super constructor
    118     y = 0;
    119   }
    120 
    121   /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>(int) register (after)
    122   /// CHECK:      MemoryBarrier kind:StoreStore
    123   /// CHECK:      MemoryBarrier kind:StoreStore
    124   /// CHECK:      MemoryBarrier kind:StoreStore
    125   /// CHECK-NEXT: ReturnVoid
    126 
    127   /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>(int) register (after)
    128   /// CHECK-NOT:  InvokeStaticOrDirect
    129   public HaveFinalsAndInheritFromClassWithFinals(int unused) {
    130     // Should inline the super constructor and keep just one memory barrier.
    131     y = 0;
    132 
    133     // Should inline new instance and keep one barrier.
    134     new HaveFinalsAndInheritFromClassWithFinals();
    135     // Should inline new instance and keep one barrier.
    136     new InheritFromClassWithFinals();
    137   }
    138 }
    139 
    140 public class Main {
    141 
    142   /// CHECK-START: ClassWithFinals Main.noInlineNoConstructorBarrier() register (after)
    143   /// CHECK:      InvokeStaticOrDirect
    144 
    145   /// CHECK-START: ClassWithFinals Main.noInlineNoConstructorBarrier() register (after)
    146   /// CHECK-NOT:  MemoryBarrier kind:StoreStore
    147   public static ClassWithFinals noInlineNoConstructorBarrier() {
    148     return new ClassWithFinals(false);
    149   }
    150 
    151   /// CHECK-START: void Main.inlineNew() register (after)
    152   /// CHECK:      MemoryBarrier kind:StoreStore
    153   /// CHECK-NEXT: ReturnVoid
    154 
    155   /// CHECK-START: void Main.inlineNew() register (after)
    156   /// CHECK-NOT:  InvokeStaticOrDirect
    157   public static void inlineNew() {
    158     new ClassWithFinals();
    159   }
    160 
    161   /// CHECK-START: void Main.inlineNew1() register (after)
    162   /// CHECK:      MemoryBarrier kind:StoreStore
    163   /// CHECK-NEXT: ReturnVoid
    164 
    165   /// CHECK-START: void Main.inlineNew1() register (after)
    166   /// CHECK-NOT:  InvokeStaticOrDirect
    167   public static void inlineNew1() {
    168     new InheritFromClassWithFinals();
    169   }
    170 
    171   /// CHECK-START: void Main.inlineNew2() register (after)
    172   /// CHECK:      MemoryBarrier kind:StoreStore
    173   /// CHECK-NEXT: ReturnVoid
    174 
    175   /// CHECK-START: void Main.inlineNew2() register (after)
    176   /// CHECK-NOT:  InvokeStaticOrDirect
    177   public static void inlineNew2() {
    178     new HaveFinalsAndInheritFromClassWithFinals();
    179   }
    180 
    181   /// CHECK-START: void Main.inlineNew3() register (after)
    182   /// CHECK:      MemoryBarrier kind:StoreStore
    183   /// CHECK:      MemoryBarrier kind:StoreStore
    184   /// CHECK-NEXT: ReturnVoid
    185 
    186   /// CHECK-START: void Main.inlineNew3() register (after)
    187   /// CHECK-NOT:  InvokeStaticOrDirect
    188   public static void inlineNew3() {
    189     new HaveFinalsAndInheritFromClassWithFinals();
    190     new HaveFinalsAndInheritFromClassWithFinals();
    191   }
    192 
    193   public static void main(String[] args) {}
    194 }
    195