1 /*
 2  * Copyright Amazon.com Inc. 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.c2.irTests;
25 
26 import jdk.test.lib.Asserts;
27 import compiler.lib.ir_framework.*;
28 
29 /*
30  * @test
31  * @bug 8286104
32  * @summary Test that C2 uses aggressive liveness to get rid of the boxing object which is
33  *  only consumed by uncommon_trap.
34  * @library /test/lib /
35  * @run driver compiler.c2.irTests.TestOptimizeUnstableIf
36  */
37 public class TestOptimizeUnstableIf {
38 
39     public static void main(String[] args) {
40         TestFramework.run();
41     }
42 
43     @Test
44     @Arguments(values = {Argument.MAX}) // the argument needs to be big enough to fall out of cache.
45     @IR(failOn = {IRNode.ALLOC_OF, "Integer"})
46     public static int boxing_object(int value) {
47         Integer ii = Integer.valueOf(value);
48         int sum = 0;
49 
50         if (value > 999_999) {
51             sum += ii.intValue();
52         }
53 
54         return sum;
55     }
56 
57     @Check(test = "boxing_object")
58     public void checkWithTestInfo(int result, TestInfo info) {
59         if (info.isWarmUp()) {
60             // Accessing the cached boxing object during warm-up phase. It prevents parser from pruning that branch of Interger.valueOf();
61             // This guarantees that a phi node is generated, which merge a cached object and the newly allocated object. eg.
62             // 112:  Phi  ===  108  168  188  [[ 50 ]]  #java/lang/Integer:NotNull:exact *  Oop:java/lang/Integer:NotNull:exact *
63             // 168: a cached object
64             // 188: result of AllocateNode
65             //  50: uncommon_trap unstable_if
66             value += Integer.valueOf(0);
67         }
68 
69         Asserts.assertEQ(result, Integer.MAX_VALUE);
70     }
71 
72     public static Integer value = Integer.valueOf(0);
73 }