1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2021, Huawei Technologies Co., Ltd. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /*
 26  * @test
 27  * @bug 8261137
 28  * @requires vm.flavor == "server"
 29  * @enablePreview
 30  * @summary Verify that box object content matches after deoptimization when it is eliminated.
 31  * @library /test/lib
 32  *
 33  * @run main/othervm -Xbatch compiler.eliminateAutobox.TestIdentityWithEliminateBoxInDebugInfo
 34  */
 35 
 36 package compiler.eliminateAutobox;
 37 
 38 import jdk.test.lib.Asserts;
 39 
 40 public class TestIdentityWithEliminateBoxInDebugInfo {
 41     interface TestF {
 42         void apply(boolean condition);
 43     }
 44 
 45     public static void helper(TestF f) {
 46         // warmup
 47         for (int i = 0; i < 100000; i++) {
 48             f.apply(true);
 49         }
 50         // deoptimize
 51         f.apply(false);
 52     }
 53 
 54     public static void runTest() throws Exception {
 55         helper((c) -> {
 56             Integer a = Integer.valueOf(42);
 57             Integer b = Integer.valueOf(-42);
 58             if (!c) {
 59                 Asserts.assertTrue(a == Integer.valueOf(42));
 60                 Asserts.assertTrue(b == Integer.valueOf(-42));
 61             }
 62         });
 63 
 64         helper((c) -> {
 65             long highBitsOnly = 2L << 40;
 66             Long a = Long.valueOf(42L);
 67             Long b = Long.valueOf(-42L);
 68             Long h = Long.valueOf(highBitsOnly);
 69             if (!c) {
 70                 Asserts.assertTrue(a == Long.valueOf(42L));
 71                 Asserts.assertTrue(b == Long.valueOf(-42L));
 72                 Asserts.assertTrue(h == Long.valueOf(highBitsOnly));
 73             }
 74         });
 75 
 76         helper((c) -> {
 77             Character a = Character.valueOf('a');
 78             Character b = Character.valueOf('Z');
 79             if (!c) {
 80                 Asserts.assertTrue(a == Character.valueOf('a'));
 81                 Asserts.assertTrue(b == Character.valueOf('Z'));
 82             }
 83         });
 84 
 85         helper((c) -> {
 86             Short a = Short.valueOf((short)42);
 87             Short b = Short.valueOf((short)-42);
 88             if (!c) {
 89                 Asserts.assertTrue(a == Short.valueOf((short)42));
 90                 Asserts.assertTrue(b == Short.valueOf((short)-42));
 91             }
 92         });
 93 
 94         helper((c) -> {
 95             Byte a = Byte.valueOf((byte)42);
 96             Byte b = Byte.valueOf((byte)-42);
 97             if (!c) {
 98                 Asserts.assertTrue(a == Byte.valueOf((byte)42));
 99                 Asserts.assertTrue(b == Byte.valueOf((byte)-42));
100             }
101         });
102 
103         helper((c) -> {
104             Boolean a = Boolean.valueOf(true);
105             Boolean b = Boolean.valueOf(false);
106             if (!c) {
107                 Asserts.assertTrue(a == Boolean.valueOf(true));
108                 Asserts.assertTrue(b == Boolean.valueOf(false));
109             }
110         });
111     }
112 
113     public static void main(String[] args) throws Exception {
114         runTest();
115     }
116 }