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({Argument.MAX}) // the argument needs to be big enough to fall out of cache.
45     // TODO 8328675 Re-enable
46     // @IR(failOn = {IRNode.ALLOC_OF, "Integer"})
47     public static int boxing_object(int value) {
48         Integer ii = Integer.valueOf(value);
49         int sum = 0;
50 
51         if (value > 999_999) {
52             sum += ii.intValue();
53         }
54 
55         return sum;
56     }
57 
58     @Check(test = "boxing_object")
59     public void checkWithTestInfo(int result, TestInfo info) {
60         if (info.isWarmUp()) {
61             // Accessing the cached boxing object during warm-up phase. It prevents parser from pruning that branch of Interger.valueOf();
62             // This guarantees that a phi node is generated, which merge a cached object and the newly allocated object. eg.
63             // 112:  Phi  ===  108  168  188  [[ 50 ]]  #java/lang/Integer:NotNull:exact *  Oop:java/lang/Integer:NotNull:exact *
64             // 168: a cached object
65             // 188: result of AllocateNode
66             //  50: uncommon_trap unstable_if
67             value += Integer.valueOf(0);
68         }
69 
70         Asserts.assertEQ(result, Integer.MAX_VALUE);
71     }
72 
73     public static Integer value = Integer.valueOf(0);
74 }