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     @Test
  57     public void testOSR04() {
  58         testOSR(getInitialOptions(), "testDeoptAfterCountedLoop");
  59     }
  60 
  61     static int limit = 10000;
  62 
  63     public static int sideEffect;
  64 
  65     public static ReturnValue testReduceLoop() {
  66         for (int i = 0; i < limit * limit; i++) {
  67             GraalDirectives.blackhole(i);
  68             if (GraalDirectives.inCompiledCode()) {
  69                 return ReturnValue.SUCCESS;
  70             }
  71         }
  72         return ReturnValue.FAILURE;
  73     }
  74 
  75     public static ReturnValue testSequentialLoop() {
  76         ReturnValue ret = ReturnValue.FAILURE;
  77         for (int i = 1; i < limit * limit; i++) {
  78             GraalDirectives.blackhole(i);
  79             if (i % 7 == 0) {
  80                 ret = ReturnValue.SUCCESS;
  81             }
  82         }
  83         GraalDirectives.controlFlowAnchor();
  84         if (sideEffect == 123) {
  85             return ReturnValue.SIDE;
  86         }
  87         for (int i = 1; i < limit * limit; i++) {
  88             GraalDirectives.blackhole(i);
  89             if (i % 33 == 0) {
  90                 ret = ReturnValue.SUCCESS;
  91             }
  92         }
  93         GraalDirectives.controlFlowAnchor();
  94         return ret;
  95     }
  96 
  97     public static ReturnValue testNonReduceLoop() {
  98         ReturnValue ret = ReturnValue.FAILURE;
  99         for (int i = 0; i < limit * limit; i++) {
 100             GraalDirectives.blackhole(i);
 101             if (i % 33 == 0) {
 102                 ret = ReturnValue.SUCCESS;
 103             }
 104         }
 105         GraalDirectives.controlFlowAnchor();
 106         return ret;
 107     }
 108 
 109     public static ReturnValue testDeoptAfterCountedLoop() {
 110         long ret = 0;
 111         for (int i = 0; GraalDirectives.injectBranchProbability(1, i < limit * limit); i++) {
 112             GraalDirectives.blackhole(i);
 113             ret = GraalDirectives.opaque(i);
 114         }
 115         GraalDirectives.controlFlowAnchor();
 116         return ret + 1 == limit * limit ? ReturnValue.SUCCESS : ReturnValue.FAILURE;
 117     }
 118 }