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   public ClassWithFinals(boolean cond) {
     31     x = 1;
     32     throw new RuntimeException();
     33     // should not inline this constructor
     34   }
     35 
     36   /// CHECK-START: void ClassWithFinals.<init>() register (after)
     37   /// CHECK:      MemoryBarrier kind:StoreStore
     38   /// CHECK-NEXT: ReturnVoid
     39   public ClassWithFinals() {
     40     // Exactly one constructor barrier.
     41     x = 0;
     42   }
     43 
     44   /// CHECK-START: void ClassWithFinals.<init>(int) register (after)
     45   /// CHECK:      MemoryBarrier kind:StoreStore
     46   /// CHECK:      MemoryBarrier kind:StoreStore
     47   /// CHECK-NEXT: ReturnVoid
     48   public ClassWithFinals(int x) {
     49     // This should have exactly two barriers:
     50     //   - one for the constructor
     51     //   - one for the `new` which should be inlined.
     52     obj = new ClassWithFinals();
     53     this.x = x;
     54   }
     55 }
     56 
     57 class InheritFromClassWithFinals extends ClassWithFinals {
     58   /// CHECK-START: void InheritFromClassWithFinals.<init>() register (after)
     59   /// CHECK:      MemoryBarrier kind:StoreStore
     60   /// CHECK-NEXT: ReturnVoid
     61 
     62   /// CHECK-START: void InheritFromClassWithFinals.<init>() register (after)
     63   /// CHECK-NOT:  InvokeStaticOrDirect
     64   public InheritFromClassWithFinals() {
     65     // Should inline the super constructor.
     66     //
     67     // Exactly one constructor barrier here.
     68   }
     69 
     70   /// CHECK-START: void InheritFromClassWithFinals.<init>(boolean) register (after)
     71   /// CHECK:      InvokeStaticOrDirect
     72 
     73   /// CHECK-START: void InheritFromClassWithFinals.<init>(boolean) register (after)
     74   /// CHECK-NOT:  MemoryBarrier kind:StoreStore
     75   public InheritFromClassWithFinals(boolean cond) {
     76     super(cond);
     77     // should not inline the super constructor
     78   }
     79 
     80   /// CHECK-START: void InheritFromClassWithFinals.<init>(int) register (after)
     81   /// CHECK:      MemoryBarrier kind:StoreStore
     82   /// CHECK:      MemoryBarrier kind:StoreStore
     83   /// CHECK-NOT:  MemoryBarrier kind:StoreStore
     84   /// CHECK:      ReturnVoid
     85 
     86   /// CHECK-START: void InheritFromClassWithFinals.<init>(int) register (after)
     87   /// CHECK-NOT:  InvokeStaticOrDirect
     88   public InheritFromClassWithFinals(int unused) {
     89     // Should inline the super constructor and insert a memory barrier.
     90 
     91     // Should inline the new instance call and insert another memory barrier.
     92     new InheritFromClassWithFinals();
     93   }
     94 }
     95 
     96 class HaveFinalsAndInheritFromClassWithFinals extends ClassWithFinals {
     97   final int y;
     98 
     99   /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>() register (after)
    100   /// CHECK:      MemoryBarrier kind:StoreStore
    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 keep 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:      MemoryBarrier kind:StoreStore
    126   /// CHECK:      MemoryBarrier kind:StoreStore
    127   /// CHECK-NEXT: ReturnVoid
    128 
    129   /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>(int) register (after)
    130   /// CHECK-NOT:  InvokeStaticOrDirect
    131   public HaveFinalsAndInheritFromClassWithFinals(int unused) {
    132     // Should inline the super constructor and keep keep both memory barriers.
    133     y = 0;
    134 
    135     // Should inline new instance and keep both memory barriers.
    136     new HaveFinalsAndInheritFromClassWithFinals();
    137     // Should inline new instance and have exactly one barrier.
    138     new InheritFromClassWithFinals();
    139   }
    140 }
    141 
    142 public class Main {
    143 
    144   /// CHECK-START: ClassWithFinals Main.noInlineNoConstructorBarrier() register (after)
    145   /// CHECK:      InvokeStaticOrDirect
    146 
    147   /// CHECK-START: ClassWithFinals Main.noInlineNoConstructorBarrier() register (after)
    148   /// CHECK-NOT:  MemoryBarrier kind:StoreStore
    149   public static ClassWithFinals noInlineNoConstructorBarrier() {
    150     return new ClassWithFinals(false);
    151     // should not inline the constructor
    152   }
    153 
    154   /// CHECK-START: void Main.inlineNew() register (after)
    155   /// CHECK:      MemoryBarrier kind:StoreStore
    156   /// CHECK-NEXT: ReturnVoid
    157 
    158   /// CHECK-START: void Main.inlineNew() register (after)
    159   /// CHECK-NOT:  InvokeStaticOrDirect
    160   public static void inlineNew() {
    161     new ClassWithFinals();
    162   }
    163 
    164   /// CHECK-START: void Main.inlineNew1() register (after)
    165   /// CHECK:      MemoryBarrier kind:StoreStore
    166   /// CHECK-NEXT: ReturnVoid
    167 
    168   /// CHECK-START: void Main.inlineNew1() register (after)
    169   /// CHECK-NOT:  InvokeStaticOrDirect
    170   public static void inlineNew1() {
    171     new InheritFromClassWithFinals();
    172   }
    173 
    174   /// CHECK-START: void Main.inlineNew2() register (after)
    175   /// CHECK:      MemoryBarrier kind:StoreStore
    176   /// CHECK:      MemoryBarrier kind:StoreStore
    177   /// CHECK-NEXT: ReturnVoid
    178 
    179   /// CHECK-START: void Main.inlineNew2() register (after)
    180   /// CHECK-NOT:  InvokeStaticOrDirect
    181   public static void inlineNew2() {
    182     new HaveFinalsAndInheritFromClassWithFinals();
    183   }
    184 
    185   /// CHECK-START: void Main.inlineNew3() register (after)
    186   /// CHECK:      MemoryBarrier kind:StoreStore
    187   /// CHECK:      MemoryBarrier kind:StoreStore
    188   /// CHECK:      MemoryBarrier kind:StoreStore
    189   /// CHECK:      MemoryBarrier kind:StoreStore
    190   /// CHECK-NEXT: ReturnVoid
    191 
    192   /// CHECK-START: void Main.inlineNew3() register (after)
    193   /// CHECK-NOT:  InvokeStaticOrDirect
    194   public static void inlineNew3() {
    195     new HaveFinalsAndInheritFromClassWithFinals();
    196     new HaveFinalsAndInheritFromClassWithFinals();
    197   }
    198 
    199   public static void main(String[] args) {}
    200 }
    201