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.
   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 package org.openjdk.bench.vm.compiler;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Param;
  30 import org.openjdk.jmh.annotations.Scope;
  31 import org.openjdk.jmh.annotations.State;
  32 
  33 import java.util.concurrent.TimeUnit;
  34 
  35 /**
  36  * Test spill code generation.
  37  */
  38 @BenchmarkMode(Mode.AverageTime)
  39 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  40 @State(Scope.Thread)
  41 public class SpillCode {
  42 
  43     @Param("10")
  44     private int iterations;
  45 
  46     private int dummy;
  47 
  48     private int doSomeCalcsInLargeBlockWithInts(int start, int iter) {
  49 
  50         int a1, a2, a3, a4;
  51         int b1, b2, b3, b4;
  52         int c1, c2, c3, c4;
  53         int d1, d2, d3, d4;
  54         int e1, e2, e3, e4;
  55         int f1, f2, f3, f4;
  56         int g1, g2, g3, g4;
  57         int h1, h2, h3, h4;
  58 
  59         // a1 = b1 = c1 = d1 = e1 = f1 = g1 = h1 = (start % 2);
  60         a2 = b2 = c2 = d2 = e2 = f2 = g2 = h2 = (start % 2);
  61         a3 = b3 = c3 = d3 = e3 = f3 = g3 = h3 = (start % 4);
  62         a4 = b4 = c4 = d4 = e4 = f4 = g4 = h4 = (start % 8);
  63 
  64         for (int i = 0; i < iter; i++) {
  65 
  66             // for each section, only x1 needs to survive.
  67 
  68             // a1 = a2 = a3 = a4 = (start + a1) % 2;
  69             a1 = start;
  70             a1 = a1 + a2 + a3 + a4;
  71             a2 = a1 + a2 + a3 + a4;
  72             a3 = a1 + a2 + a3 + a4;
  73             a4 = a1 + a2 + a3 + a4;
  74             a1 = a1 + a2 + a3 + a4;
  75             a2 = a1 + a2 + a3 + a4;
  76             a3 = a1 + a2 + a3 + a4;
  77             a4 = a1 + a2 + a3 + a4;
  78             a1 = a1 + a2 + a3 + a4;
  79             a2 = a1 + a2 + a3 + a4;
  80             a3 = a1 + a2 + a3 + a4;
  81             a4 = a1 + a2 + a3 + a4;
  82 
  83             // b1 = b2 = b3 = b4 = (a4 + b1) % 2;
  84             b1 = a4;
  85             b1 = b1 + b2 + b3 + b4;
  86             b2 = b1 + b2 + b3 + b4;
  87             b3 = b1 + b2 + b3 + b4;
  88             b4 = b1 + b2 + b3 + b4;
  89             b1 = b1 + b2 + b3 + b4;
  90             b2 = b1 + b2 + b3 + b4;
  91             b3 = b1 + b2 + b3 + b4;
  92             b4 = b1 + b2 + b3 + b4;
  93             b1 = b1 + b2 + b3 + b4;
  94             b2 = b1 + b2 + b3 + b4;
  95             b3 = b1 + b2 + b3 + b4;
  96             b4 = b1 + b2 + b3 + b4;
  97 
  98             // c1 = c2 = c3 = c4 = (b4 + c1) % 2;
  99             c1 = b4;
 100             c1 = c1 + c2 + c3 + c4;
 101             c2 = c1 + c2 + c3 + c4;
 102             c3 = c1 + c2 + c3 + c4;
 103             c4 = c1 + c2 + c3 + c4;
 104             c1 = c1 + c2 + c3 + c4;
 105             c2 = c1 + c2 + c3 + c4;
 106             c3 = c1 + c2 + c3 + c4;
 107             c4 = c1 + c2 + c3 + c4;
 108             c1 = c1 + c2 + c3 + c4;
 109             c2 = c1 + c2 + c3 + c4;
 110             c3 = c1 + c2 + c3 + c4;
 111             c4 = c1 + c2 + c3 + c4;
 112 
 113             // d1 = d2 = d3 = d4 = (c4 + d1) % 2;
 114             d1 = c4;
 115             d1 = d1 + d2 + d3 + d4;
 116             d2 = d1 + d2 + d3 + d4;
 117             d3 = d1 + d2 + d3 + d4;
 118             d4 = d1 + d2 + d3 + d4;
 119             d1 = d1 + d2 + d3 + d4;
 120             d2 = d1 + d2 + d3 + d4;
 121             d3 = d1 + d2 + d3 + d4;
 122             d4 = d1 + d2 + d3 + d4;
 123             d1 = d1 + d2 + d3 + d4;
 124             d2 = d1 + d2 + d3 + d4;
 125             d3 = d1 + d2 + d3 + d4;
 126             d4 = d1 + d2 + d3 + d4;
 127 
 128             // e1 = e2 = e3 = e4 = (d4 + e1) % 2;
 129             e1 = d4;
 130             e1 = e1 + e2 + e3 + e4;
 131             e2 = e1 + e2 + e3 + e4;
 132             e3 = e1 + e2 + e3 + e4;
 133             e4 = e1 + e2 + e3 + e4;
 134             e1 = e1 + e2 + e3 + e4;
 135             e2 = e1 + e2 + e3 + e4;
 136             e3 = e1 + e2 + e3 + e4;
 137             e4 = e1 + e2 + e3 + e4;
 138             e1 = e1 + e2 + e3 + e4;
 139             e2 = e1 + e2 + e3 + e4;
 140             e3 = e1 + e2 + e3 + e4;
 141             e4 = e1 + e2 + e3 + e4;
 142 
 143             // f1 = f2 = f3 = f4 = (e4 + f1) % 2;
 144             f1 = e4;
 145             f1 = f1 + f2 + f3 + f4;
 146             f2 = f1 + f2 + f3 + f4;
 147             f3 = f1 + f2 + f3 + f4;
 148             f4 = f1 + f2 + f3 + f4;
 149             f1 = f1 + f2 + f3 + f4;
 150             f2 = f1 + f2 + f3 + f4;
 151             f3 = f1 + f2 + f3 + f4;
 152             f4 = f1 + f2 + f3 + f4;
 153             f1 = f1 + f2 + f3 + f4;
 154             f2 = f1 + f2 + f3 + f4;
 155             f3 = f1 + f2 + f3 + f4;
 156             f4 = f1 + f2 + f3 + f4;
 157 
 158             // g1 = g2 = g3 = g4 = (f4 + g1) % 2;
 159             g1 = f4;
 160             g1 = g1 + g2 + g3 + g4;
 161             g2 = g1 + g2 + g3 + g4;
 162             g3 = g1 + g2 + g3 + g4;
 163             g4 = g1 + g2 + g3 + g4;
 164             g1 = g1 + g2 + g3 + g4;
 165             g2 = g1 + g2 + g3 + g4;
 166             g3 = g1 + g2 + g3 + g4;
 167             g4 = g1 + g2 + g3 + g4;
 168             g1 = g1 + g2 + g3 + g4;
 169             g2 = g1 + g2 + g3 + g4;
 170             g3 = g1 + g2 + g3 + g4;
 171             g4 = g1 + g2 + g3 + g4;
 172 
 173             // h1 = h2 = h3 = h4 = (g4 + h1) % 2;
 174             h1 = g4;
 175             h1 = h1 + h2 + h3 + h4;
 176             h2 = h1 + h2 + h3 + h4;
 177             h3 = h1 + h2 + h3 + h4;
 178             h4 = h1 + h2 + h3 + h4;
 179             h1 = h1 + h2 + h3 + h4;
 180             h2 = h1 + h2 + h3 + h4;
 181             h3 = h1 + h2 + h3 + h4;
 182             h4 = h1 + h2 + h3 + h4;
 183             h1 = h1 + h2 + h3 + h4;
 184             h2 = h1 + h2 + h3 + h4;
 185             h3 = h1 + h2 + h3 + h4;
 186             h4 = h1 + h2 + h3 + h4;
 187 
 188             start = h4;
 189         }
 190 
 191         return start;
 192     }
 193 
 194     /**
 195      * The test runs a loop with many local variables. The issue it reproduces is that if handled wrong, too many variables
 196      * are put on and referenced on stack. The number of iterations is taken from global variable, to prevent static loop
 197      * unrolling. Many of the variables used in the larger loop are local inside the block and do dnot need to survive from
 198      * one iteration to the next.
 199      */
 200     @Benchmark
 201     public int testSpillForManyInts() throws Exception {
 202         return doSomeCalcsInLargeBlockWithInts(dummy, iterations);
 203     }
 204 }