1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package compiler.cha;
  25 
  26 import jdk.internal.vm.annotation.DontInline;
  27 import static compiler.cha.StrengthReduceInterfaceCall.*;
  28 
  29 public class ThreeLevelHierarchyLinear extends ATest<ThreeLevelHierarchyLinear.I> {
  30     public ThreeLevelHierarchyLinear() {
  31         super(I.class, C.class);
  32     }
  33 
  34     interface J           { Object m(); }
  35     interface I extends J {}
  36 
  37     interface K1 extends I {}
  38     interface K2 extends I { Object m(); }
  39     interface K3 extends I { default Object m() { return WRONG; }}
  40 
  41     static class C  implements I { public Object m() { return CORRECT; }}
  42 
  43     static class DI implements I { public Object m() { return WRONG;   }}
  44     static class DJ implements J { public Object m() { return WRONG;   }}
  45 
  46     @DontInline
  47     public Object test(I i) {
  48         return i.m(); // I <: J.m ABSTRACT
  49     }
  50 
  51     @TestCase
  52     public void testMega1() {
  53         // 0. Trigger compilation of a megamorphic call site
  54         compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I <: intf J.m ABSTRACT
  55         assertCompiled();
  56 
  57         // Dependency: type = unique_concrete_method, context = I, method = C.m
  58 
  59         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
  60 
  61         // 1. No deoptimization/invalidation on not-yet-seen receiver
  62         repeat(100, () -> call(new C(){})); // Cn <: C.m <: intf I
  63         assertCompiled(); // No deopt on not-yet-seen receiver
  64 
  65         // 2. No dependency invalidation: different context
  66         initialize(DJ.class,  //      DJ.m                    <: intf J.m ABSTRACT
  67                    K1.class,  // intf K1            <: intf I <: intf J.m ABSTRACT
  68                    K2.class); // intf K2.m ABSTRACT <: intf I <: intf J.m ABSTRACT
  69         assertCompiled();
  70 
  71         // 3. Dependency invalidation: DI.m <: I
  72         initialize(DI.class); //      DI.m          <: intf I <: intf J.m ABSTRACT
  73         assertNotCompiled();
  74 
  75         // 4. Recompilation w/o a dependency
  76         compile(megamorphic());
  77         call(new C() { public Object m() { return CORRECT; }}); // Cn.m <: C.m <: intf I <: intf J.m ABSTRACT
  78 
  79         assertCompiled(); // no dependency
  80 
  81         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
  82     }
  83 
  84     @TestCase
  85     public void testMega2() {
  86         compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I <: intf J.m ABSTRACT
  87         assertCompiled();
  88 
  89         // Dependency: type = unique_concrete_method, context = I, method = C.m
  90 
  91         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
  92 
  93         // Dependency invalidation
  94         initialize(K3.class); // intf K3.m DEFAULT <: intf I;
  95         assertNotCompiled(); // FIXME: default methods in sub-interfaces shouldn't be taken into account by CHA
  96 
  97         // Recompilation with a dependency
  98         compile(megamorphic());
  99         assertCompiled();
 100 
 101         // Dependency: type = unique_concrete_method, context = I, method = C.m
 102 
 103         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
 104 
 105         call(new K3() { public Object m() { return CORRECT; }}); // Kn.m <: K3.m DEFAULT <: intf I <: intf J.m ABSTRACT
 106         assertNotCompiled();
 107 
 108         // Recompilation w/o a dependency
 109         compile(megamorphic());
 110         // Dependency: none
 111         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
 112         call(new C() { public Object m() { return CORRECT; }}); // Cn.m <: C.m <: intf I <: intf J.m ABSTRACT
 113         assertCompiled();
 114     }
 115 
 116     @Override
 117     public void checkInvalidReceiver() {
 118         shouldThrow(IncompatibleClassChangeError.class, () -> {
 119             I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
 120             test(o);
 121         });
 122         assertCompiled();
 123 
 124         shouldThrow(IncompatibleClassChangeError.class, () -> {
 125             I j = (I) unsafeCastMH(I.class).invokeExact((Object) new J() {
 126                 public Object m() {
 127                     return WRONG;
 128                 }
 129             }); // super interface
 130             test(j);
 131         });
 132         assertCompiled();
 133     }
 134 }