1 /*
   2  * Copyright (c) 2011, 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 /*
  26  * @test
  27  *
  28  * @summary converted from VM Testbase vm/mlvm/meth/stress/compiler/inlineMHTarget.
  29  * VM Testbase keywords: [feature_mlvm, nonconcurrent]
  30  * VM Testbase readme:
  31  * DESCRIPTION
  32  *     The test creates MH to short methods that are likely to be inlined and
  33  *     verifies that they are inlined into MH code correctly.
  34  *     See vm.mlvm.meth.stress.java.sequences.Test for details on MH sequences.
  35  *
  36  * @library /vmTestbase
  37  *          /test/lib
  38  * @run driver jdk.test.lib.FileInstaller . .
  39  *
  40  * @comment build test class and indify classes
  41  * @build vm.mlvm.meth.stress.compiler.inlineMHTarget.Test
  42  * @run driver vm.mlvm.share.IndifiedClassesBuilder
  43  *
  44  * @run main/othervm vm.mlvm.meth.stress.compiler.inlineMHTarget.Test -stressIterationsFactor 100
  45  */
  46 
  47 package vm.mlvm.meth.stress.compiler.inlineMHTarget;
  48 
  49 import java.lang.invoke.MethodHandle;
  50 import java.lang.invoke.MethodHandles;
  51 import java.lang.invoke.MethodHandles.Lookup;
  52 import java.lang.invoke.MethodType;
  53 
  54 import nsk.share.test.Stresser;
  55 import vm.mlvm.share.Env;
  56 import vm.mlvm.share.MlvmTest;
  57 
  58 // TODO: check compilation using vm.mlvm.share.comp framework
  59 // TODO: enhance to check NxN primitive types
  60 public class Test extends MlvmTest {
  61 
  62     private static final int ITERATIONS = 10000; // Should be enough to trigger compilation
  63 
  64     private static final int THE_CONSTANT = 42;
  65     private int field = 96;
  66 
  67     static int i(int i) { return i; }
  68     static int k(int i) { return THE_CONSTANT;  }
  69     int getter() { return this.field; }
  70 
  71     int iplusk(int i) { return i(i) + k(i) + getter(); }
  72 
  73     static int mh_iplusk(MethodHandle a, MethodHandle b, MethodHandle c, int i) throws Throwable {
  74         return (int) a.invokeExact(i) + (int) b.invokeExact(i) + (int) c.invokeExact();
  75     }
  76 
  77     @Override
  78     public boolean run() throws Throwable {
  79         Lookup l = MethodHandles.lookup();
  80         MethodHandle ipluskMH = l.findVirtual(
  81                     Test.class, "iplusk", MethodType.methodType(int.class, int.class)
  82                 ).bindTo(this);
  83 
  84         MethodHandle iMH = MethodHandles.identity(int.class);
  85         MethodHandle kMH = MethodHandles.dropArguments(MethodHandles.constant(int.class, THE_CONSTANT), 0, int.class);
  86         MethodHandle getterMH = l.findGetter(Test.class, "field", int.class).bindTo(this);
  87         MethodHandle mh_ipluskMH = MethodHandles.insertArguments(
  88                 l.findStatic(Test.class, "mh_iplusk", MethodType.methodType(int.class, MethodHandle.class, MethodHandle.class, MethodHandle.class, int.class)),
  89                 0, iMH, kMH, getterMH);
  90 
  91         int mh1Result = 0, mh2Result = 0, directResult = 0;
  92         Stresser s = createStresser();
  93         s.start(1);
  94         try {
  95             while ( s.continueExecution() ) {
  96                 for (int i = 0; i < ITERATIONS; i++) {
  97                     s.iteration();
  98                     mh1Result += (int) ipluskMH.invokeExact(i);
  99                     mh2Result += (int) mh_ipluskMH.invokeExact(i);
 100                     directResult += iplusk(i);
 101                 }
 102             }
 103         } finally {
 104             s.finish();
 105         }
 106 
 107         if (mh1Result != directResult || mh2Result != directResult) {
 108             Env.complain("Sum computed using MH 1=" + mh1Result
 109                        + "; Sum computed using MH 2=" + mh2Result
 110                        + "; using direct calls=" + directResult);
 111             return false;
 112         }
 113 
 114         return true;
 115     }
 116 
 117     public static void main(String[] args) {
 118         MlvmTest.launch(args);
 119     }
 120 }