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.Scope;
  30 import org.openjdk.jmh.annotations.State;
  31 
  32 import java.util.concurrent.TimeUnit;
  33 
  34 /**
  35  * Tests for removal of redundant stores. It's crucial for the tests to be valid that inlining and allocation is
  36  * performed as described in this file.
  37  */
  38 @BenchmarkMode(Mode.AverageTime)
  39 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  40 @State(Scope.Thread)
  41 public class StoreAfterStore {
  42     public int s1 = 1, s2 = 2, s3 = 3, s4 = 4, s5 = 5, s6 = 6, s7 = 7, s8 = 8;
  43 
  44     /**
  45      * Test removal of redundant zero stores following an object allocation.
  46      */
  47     @Benchmark
  48     public AllocAndZeroStoreHelper testAllocAndZeroStore() throws Exception {
  49         return new AllocAndZeroStoreHelper(s1, s2, s3, s4, s5, s6, s7, s8);
  50     }
  51 
  52     /**
  53      * Test removal of stores followed by stores to the same memory location.
  54      */
  55     @Benchmark
  56     public StoreAndStoreHelper testStoreAndStore() throws Exception {
  57         return new StoreAndStoreHelper(s1, s2, s3, s4, s5, s6, s7, s8);
  58     }
  59 
  60 
  61     /**
  62      * Helper for alloc followed by zero store testing.
  63      */
  64     static class AllocAndZeroStoreHelper {
  65         public volatile int vf1, vf2, vf3, vf4, vf5, vf6, vf7, vf8;
  66 
  67         public static int s1, s2, s3, s4, s5, s6, s7, s8;
  68 
  69         private AllocAndZeroStoreHelper() {
  70             this.vf1 = 0;
  71             this.vf2 = 0;
  72             this.vf3 = 0;
  73             this.vf4 = 0;
  74             this.vf5 = 0;
  75             this.vf6 = 0;
  76             this.vf7 = 0;
  77             this.vf8 = 0;
  78         }
  79 
  80         private AllocAndZeroStoreHelper(int l1, int l2, int l3, int l4, int l5, int l6, int l7, int l8) {
  81             this(); // Redundant initialization to zero here
  82 
  83             // dummy stores
  84             s1 = l1;
  85             s2 = l2;
  86             s3 = l3;
  87             s4 = l4;
  88             s5 = l5;
  89             s6 = l6;
  90             s7 = l7;
  91             s8 = l8;
  92         }
  93 
  94     }
  95 
  96     /**
  97      * Helper for store made redundant by subsequent store testing.
  98      */
  99     static class StoreAndStoreHelper {
 100         public volatile int vf1, vf2, vf3, vf4, vf5, vf6, vf7, vf8;
 101 
 102         private StoreAndStoreHelper() {
 103             this.vf1 = -1;
 104             this.vf2 = -1;
 105             this.vf3 = -1;
 106             this.vf4 = -1;
 107             this.vf5 = -1;
 108             this.vf6 = -1;
 109             this.vf7 = -1;
 110             this.vf8 = -1;
 111         }
 112 
 113         private StoreAndStoreHelper(int l1, int l2, int l3, int l4, int l5, int l6, int l7, int l8) {
 114             this(); // Initialize all to -1 here, redundant wrt to the below stores
 115 
 116             this.vf1 = l1;
 117             this.vf2 = l2;
 118             this.vf3 = l3;
 119             this.vf4 = l4;
 120             this.vf5 = l5;
 121             this.vf6 = l6;
 122             this.vf7 = l7;
 123             this.vf8 = l8;
 124         }
 125 
 126     }
 127 }