1 /*
  2  * Copyright (c) 2022, Red Hat, Inc. 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  * @test
 26  * @bug 8296545
 27  * @requires vm.compiler2.enabled
 28  * @summary Blackholes should allow load optimizations
 29  * @library /test/lib /
 30  * @run driver compiler.c2.irTests.blackhole.BlackholeLoadOptoTest
 31  */
 32 
 33 package compiler.c2.irTests.blackhole;
 34 
 35 import compiler.lib.ir_framework.*;
 36 import jdk.test.lib.Asserts;
 37 
 38 public class BlackholeLoadOptoTest {
 39 
 40     public static void main(String[] args) {
 41         TestFramework.runWithFlags(
 42             "-XX:+UnlockExperimentalVMOptions",
 43             "-XX:CompileThreshold=100",
 44             "-XX:-TieredCompilation",
 45             "-XX:CompileCommand=blackhole,compiler.c2.irTests.blackhole.BlackholeLoadOptoTest::blackhole",
 46             "-XX:CompileCommand=dontinline,compiler.c2.irTests.blackhole.BlackholeLoadOptoTest::dontinline"
 47         );
 48     }
 49 
 50     static int x, y;
 51 
 52 
 53     /*
 54      * Negative test: check that dangling expressions are eliminated
 55      */
 56 
 57     @Test
 58     @IR(failOn = {IRNode.LOAD_I, IRNode.MUL_I})
 59     static void testNothing() {
 60         int r1 = x * y;
 61         int r2 = x * y;
 62     }
 63 
 64     @Run(test = "testNothing")
 65     static void runNothing() {
 66         testNothing();
 67     }
 68 
 69     /*
 70      * Auxiliary test: check that dontinline method does break optimizations
 71      */
 72 
 73     @Test
 74     @IR(counts = {IRNode.LOAD_I, "4"})
 75     @IR(counts = {IRNode.MUL_I, "2"})
 76     static void testDontline() {
 77         int r1 = x * y;
 78         dontinline(r1);
 79         int r2 = x * y;
 80         dontinline(r2);
 81     }
 82 
 83     static void dontinline(int x) {}
 84 
 85     @Run(test = "testDontline")
 86     static void runDontinline() {
 87         testDontline();
 88     }
 89 
 90     /*
 91      * Positive test: check that blackhole does not break optimizations
 92      */
 93 
 94     @Test
 95     @IR(counts = {IRNode.LOAD_I, "2"})
 96     @IR(counts = {IRNode.MUL_I, "1"})
 97     static void testBlackholed() {
 98         int r1 = x * y;
 99         blackhole(r1);
100         int r2 = x * y;
101         blackhole(r2);
102     }
103 
104     static void blackhole(int x) {}
105 
106     @Run(test = "testBlackholed")
107     static void runBlackholed() {
108         testBlackholed();
109     }
110 
111 }