1 /*
   2  * Copyright (c) 2016, 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 
  25 /**
  26  * @test
  27  * @bug 8150804
  28  * @summary Tests elimination of Phi nodes without losing type information.
  29  * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement TestPhiElimination
  30  */
  31 public class TestPhiElimination {
  32     /*
  33        A::get() is inlined into test(obj) producing the following graph:
  34 
  35                Parm (obj)
  36             TestPhiElimination
  37                    |
  38                  CastPP
  39         TestPhiElimination:NotNull
  40                    |
  41               CheckCastPP
  42                A:NotNull
  43                /       \
  44        CheckCastPP     |
  45         A:NotNull      |
  46                 \     /
  47                   Phi
  48                    A
  49                    |
  50                Safepoint
  51 
  52        PhiNode::ideal() then replaces the Phi by a CheckCastPP:
  53 
  54                Parm (obj)
  55             TestPhiElimination
  56                    |
  57               CheckCastPP
  58                    A
  59                    |
  60                Safepoint
  61 
  62        losing the :NotNull information. Therefore, we cannot prove that obj != null
  63        when accessing a field and add an uncommon trap. Since obj is used as monitor, we
  64        set it to TOP in the uncommon trap branch and later fail in Process_OopMap_Node
  65        because the monitor object is TOP.
  66     */
  67     public Object test(TestPhiElimination obj) {
  68         if (obj instanceof A) {
  69             return ((A) obj).get();
  70         }
  71         return null;
  72     }
  73 
  74     static public void main(String[] args) {
  75         TestPhiElimination t = new TestPhiElimination();
  76 
  77         // Warmup
  78         B b = new B();
  79         for (int i = 0; i < 1_000; ++i) {
  80             t.test(b);
  81         }
  82 
  83         // Compile
  84         A a = new A();
  85         for (int i = 0; i < 20_000; ++i) {
  86             if (i % 2 == 0) {
  87                 a.f = null;
  88             }
  89             t.test(a);
  90         }
  91     }
  92 
  93 }
  94 
  95 class A extends TestPhiElimination {
  96     public Object f;
  97 
  98     public A create() {
  99         return new A();
 100     }
 101 
 102     public synchronized Object get() {
 103         if (f == null) {
 104             f = create();
 105         }
 106         return f;
 107     }
 108 }
 109 
 110 class B extends A {
 111 
 112 }