1 /*
   2  * Copyright (c) 2014 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.bench.vm.compiler;
  26 
  27 import org.openjdk.jmh.annotations.Benchmark;
  28 import org.openjdk.jmh.annotations.BenchmarkMode;
  29 import org.openjdk.jmh.annotations.Mode;
  30 import org.openjdk.jmh.annotations.OutputTimeUnit;
  31 import org.openjdk.jmh.annotations.Param;
  32 import org.openjdk.jmh.annotations.Scope;
  33 import org.openjdk.jmh.annotations.State;
  34 
  35 import java.util.concurrent.TimeUnit;
  36 
  37 /**
  38  * Test spill code generation.
  39  */
  40 @BenchmarkMode(Mode.AverageTime)
  41 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  42 @State(Scope.Thread)
  43 public class SpillCode {
  44 
  45     @Param("10")
  46     private int iterations;
  47 
  48     private int dummy;
  49 
  50     private int doSomeCalcsInLargeBlockWithInts(int start, int iter) {
  51 
  52         int a1, a2, a3, a4;
  53         int b1, b2, b3, b4;
  54         int c1, c2, c3, c4;
  55         int d1, d2, d3, d4;
  56         int e1, e2, e3, e4;
  57         int f1, f2, f3, f4;
  58         int g1, g2, g3, g4;
  59         int h1, h2, h3, h4;
  60 
  61         // a1 = b1 = c1 = d1 = e1 = f1 = g1 = h1 = (start % 2);
  62         a2 = b2 = c2 = d2 = e2 = f2 = g2 = h2 = (start % 2);
  63         a3 = b3 = c3 = d3 = e3 = f3 = g3 = h3 = (start % 4);
  64         a4 = b4 = c4 = d4 = e4 = f4 = g4 = h4 = (start % 8);
  65 
  66         for (int i = 0; i < iter; i++) {
  67 
  68             // for each section, only x1 needs to survive.
  69 
  70             // a1 = a2 = a3 = a4 = (start + a1) % 2;
  71             a1 = start;
  72             a1 = a1 + a2 + a3 + a4;
  73             a2 = a1 + a2 + a3 + a4;
  74             a3 = a1 + a2 + a3 + a4;
  75             a4 = a1 + a2 + a3 + a4;
  76             a1 = a1 + a2 + a3 + a4;
  77             a2 = a1 + a2 + a3 + a4;
  78             a3 = a1 + a2 + a3 + a4;
  79             a4 = a1 + a2 + a3 + a4;
  80             a1 = a1 + a2 + a3 + a4;
  81             a2 = a1 + a2 + a3 + a4;
  82             a3 = a1 + a2 + a3 + a4;
  83             a4 = a1 + a2 + a3 + a4;
  84 
  85             // b1 = b2 = b3 = b4 = (a4 + b1) % 2;
  86             b1 = a4;
  87             b1 = b1 + b2 + b3 + b4;
  88             b2 = b1 + b2 + b3 + b4;
  89             b3 = b1 + b2 + b3 + b4;
  90             b4 = b1 + b2 + b3 + b4;
  91             b1 = b1 + b2 + b3 + b4;
  92             b2 = b1 + b2 + b3 + b4;
  93             b3 = b1 + b2 + b3 + b4;
  94             b4 = b1 + b2 + b3 + b4;
  95             b1 = b1 + b2 + b3 + b4;
  96             b2 = b1 + b2 + b3 + b4;
  97             b3 = b1 + b2 + b3 + b4;
  98             b4 = b1 + b2 + b3 + b4;
  99 
 100             // c1 = c2 = c3 = c4 = (b4 + c1) % 2;
 101             c1 = b4;
 102             c1 = c1 + c2 + c3 + c4;
 103             c2 = c1 + c2 + c3 + c4;
 104             c3 = c1 + c2 + c3 + c4;
 105             c4 = c1 + c2 + c3 + c4;
 106             c1 = c1 + c2 + c3 + c4;
 107             c2 = c1 + c2 + c3 + c4;
 108             c3 = c1 + c2 + c3 + c4;
 109             c4 = c1 + c2 + c3 + c4;
 110             c1 = c1 + c2 + c3 + c4;
 111             c2 = c1 + c2 + c3 + c4;
 112             c3 = c1 + c2 + c3 + c4;
 113             c4 = c1 + c2 + c3 + c4;
 114 
 115             // d1 = d2 = d3 = d4 = (c4 + d1) % 2;
 116             d1 = c4;
 117             d1 = d1 + d2 + d3 + d4;
 118             d2 = d1 + d2 + d3 + d4;
 119             d3 = d1 + d2 + d3 + d4;
 120             d4 = d1 + d2 + d3 + d4;
 121             d1 = d1 + d2 + d3 + d4;
 122             d2 = d1 + d2 + d3 + d4;
 123             d3 = d1 + d2 + d3 + d4;
 124             d4 = d1 + d2 + d3 + d4;
 125             d1 = d1 + d2 + d3 + d4;
 126             d2 = d1 + d2 + d3 + d4;
 127             d3 = d1 + d2 + d3 + d4;
 128             d4 = d1 + d2 + d3 + d4;
 129 
 130             // e1 = e2 = e3 = e4 = (d4 + e1) % 2;
 131             e1 = d4;
 132             e1 = e1 + e2 + e3 + e4;
 133             e2 = e1 + e2 + e3 + e4;
 134             e3 = e1 + e2 + e3 + e4;
 135             e4 = e1 + e2 + e3 + e4;
 136             e1 = e1 + e2 + e3 + e4;
 137             e2 = e1 + e2 + e3 + e4;
 138             e3 = e1 + e2 + e3 + e4;
 139             e4 = e1 + e2 + e3 + e4;
 140             e1 = e1 + e2 + e3 + e4;
 141             e2 = e1 + e2 + e3 + e4;
 142             e3 = e1 + e2 + e3 + e4;
 143             e4 = e1 + e2 + e3 + e4;
 144 
 145             // f1 = f2 = f3 = f4 = (e4 + f1) % 2;
 146             f1 = e4;
 147             f1 = f1 + f2 + f3 + f4;
 148             f2 = f1 + f2 + f3 + f4;
 149             f3 = f1 + f2 + f3 + f4;
 150             f4 = f1 + f2 + f3 + f4;
 151             f1 = f1 + f2 + f3 + f4;
 152             f2 = f1 + f2 + f3 + f4;
 153             f3 = f1 + f2 + f3 + f4;
 154             f4 = f1 + f2 + f3 + f4;
 155             f1 = f1 + f2 + f3 + f4;
 156             f2 = f1 + f2 + f3 + f4;
 157             f3 = f1 + f2 + f3 + f4;
 158             f4 = f1 + f2 + f3 + f4;
 159 
 160             // g1 = g2 = g3 = g4 = (f4 + g1) % 2;
 161             g1 = f4;
 162             g1 = g1 + g2 + g3 + g4;
 163             g2 = g1 + g2 + g3 + g4;
 164             g3 = g1 + g2 + g3 + g4;
 165             g4 = g1 + g2 + g3 + g4;
 166             g1 = g1 + g2 + g3 + g4;
 167             g2 = g1 + g2 + g3 + g4;
 168             g3 = g1 + g2 + g3 + g4;
 169             g4 = g1 + g2 + g3 + g4;
 170             g1 = g1 + g2 + g3 + g4;
 171             g2 = g1 + g2 + g3 + g4;
 172             g3 = g1 + g2 + g3 + g4;
 173             g4 = g1 + g2 + g3 + g4;
 174 
 175             // h1 = h2 = h3 = h4 = (g4 + h1) % 2;
 176             h1 = g4;
 177             h1 = h1 + h2 + h3 + h4;
 178             h2 = h1 + h2 + h3 + h4;
 179             h3 = h1 + h2 + h3 + h4;
 180             h4 = h1 + h2 + h3 + h4;
 181             h1 = h1 + h2 + h3 + h4;
 182             h2 = h1 + h2 + h3 + h4;
 183             h3 = h1 + h2 + h3 + h4;
 184             h4 = h1 + h2 + h3 + h4;
 185             h1 = h1 + h2 + h3 + h4;
 186             h2 = h1 + h2 + h3 + h4;
 187             h3 = h1 + h2 + h3 + h4;
 188             h4 = h1 + h2 + h3 + h4;
 189 
 190             start = h4;
 191         }
 192 
 193         return start;
 194     }
 195 
 196     /**
 197      * The test runs a loop with many local variables. The issue it reproduces is that if handled wrong, too many variables
 198      * are put on and referenced on stack. The number of iterations is taken from global variable, to prevent static loop
 199      * unrolling. Many of the variables used in the larger loop are local inside the block and do dnot need to survive from
 200      * one iteration to the next.
 201      */
 202     @Benchmark
 203     public int testSpillForManyInts() throws Exception {
 204         return doSomeCalcsInLargeBlockWithInts(dummy, iterations);
 205     }
 206 }