1 /*
   2  * Copyright (c) 2016, 2018, 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 package org.graalvm.compiler.hotspot.test;
  26 
  27 import org.graalvm.compiler.api.directives.GraalDirectives;
  28 import org.junit.Assert;
  29 import org.junit.Test;
  30 
  31 /**
  32  * Test on-stack-replacement with Graal. The test manually triggers a Graal OSR-compilation which is
  33  * later invoked when hitting the backedge counter overflow.
  34  */
  35 public class GraalOSRTest extends GraalOSRTestBase {
  36 
  37     @Test
  38     public void testOSR01() {
  39         try {
  40             testOSR(getInitialOptions(), "testReduceLoop");
  41         } catch (Throwable t) {
  42             Assert.assertEquals("OSR compilation without OSR entry loop.", t.getMessage());
  43         }
  44     }
  45 
  46     @Test
  47     public void testOSR02() {
  48         testOSR(getInitialOptions(), "testSequentialLoop");
  49     }
  50 
  51     @Test
  52     public void testOSR03() {
  53         testOSR(getInitialOptions(), "testNonReduceLoop");
  54     }
  55 
  56     static int limit = 10000;
  57 
  58     public static int sideEffect;
  59 
  60     public static ReturnValue testReduceLoop() {
  61         for (int i = 0; i < limit * limit; i++) {
  62             GraalDirectives.blackhole(i);
  63             if (GraalDirectives.inCompiledCode()) {
  64                 return ReturnValue.SUCCESS;
  65             }
  66         }
  67         return ReturnValue.FAILURE;
  68     }
  69 
  70     public static ReturnValue testSequentialLoop() {
  71         ReturnValue ret = ReturnValue.FAILURE;
  72         for (int i = 1; i < limit * limit; i++) {
  73             GraalDirectives.blackhole(i);
  74             if (i % 7 == 0) {
  75                 ret = ReturnValue.SUCCESS;
  76             }
  77         }
  78         GraalDirectives.controlFlowAnchor();
  79         if (sideEffect == 123) {
  80             return ReturnValue.SIDE;
  81         }
  82         for (int i = 1; i < limit * limit; i++) {
  83             GraalDirectives.blackhole(i);
  84             if (i % 33 == 0) {
  85                 ret = ReturnValue.SUCCESS;
  86             }
  87         }
  88         GraalDirectives.controlFlowAnchor();
  89         return ret;
  90     }
  91 
  92     public static ReturnValue testNonReduceLoop() {
  93         ReturnValue ret = ReturnValue.FAILURE;
  94         for (int i = 0; i < limit * limit; i++) {
  95             GraalDirectives.blackhole(i);
  96             if (i % 33 == 0) {
  97                 ret = ReturnValue.SUCCESS;
  98             }
  99         }
 100         GraalDirectives.controlFlowAnchor();
 101         return ret;
 102     }
 103 }