Path: blob/master/test/micro/org/openjdk/bench/vm/compiler/Straighten.java
41161 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.vm.compiler;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Scope;29import org.openjdk.jmh.annotations.Setup;30import org.openjdk.jmh.annotations.State;3132import java.util.Random;33import java.util.concurrent.TimeUnit;3435/**36* Various small benchmarks testing how well the optimizer straightens code.37*/38@BenchmarkMode(Mode.AverageTime)39@OutputTimeUnit(TimeUnit.NANOSECONDS)40@State(Scope.Thread)41public class Straighten {4243private int[] intArr;44private long[] longArr;4546@Setup47public void setupSubclass() {48int TEST_SIZE = 300;4950Random r = new Random(453543);5152/*53* initialize arrays with some values between 0 and 7.54*/55intArr = new int[TEST_SIZE];56for (int i = 0; i < TEST_SIZE; i++) {57intArr[i] = r.nextInt(8);58}5960longArr = new long[TEST_SIZE];61for (int i = 0; i < TEST_SIZE; i++) {62longArr[i] = (long) r.nextInt(8);63}6465}6667private int innerCandidate1int(int i) {68int j = 0;6970if (i == 5) {71/* we know that i isn't 4 and is less than 7 here. */72j++;73}74if (i == 4) {75/* we know that i is less than 7 here. */76j += 2;77}78if (i < 7) {79/* we know that i is less than 7, so it isn't greater than 7. */80j += 4;81}82if (i > 7) {83j += 8;84}85return j;86}8788/** Tests how well serial constant integer compares are straightened. */89@Benchmark90public int testStraighten1int() throws Exception {91int dummy = 0;92int[] arr = intArr;93for (int i : arr) {94dummy += innerCandidate1int(i);95}96return dummy;97}9899private long innerCandidate1long(long l) {100long j = 0;101102if (l == 5) {103/* we know that l isn't 4 and is less than 7 here. */104j++;105}106if (l == 4) {107/* we know that l is less than 7 here. */108j += 2;109}110if (l < 7) {111/* we know that l is less than 7, so it isn't greater than 7. */112j += 4;113}114if (l > 7) {115j += 8;116}117return j;118}119120/** Tests how well serial constant long compares are straightened. */121@Benchmark122public int testStraighten1long() throws Exception {123int dummy = 0;124long[] arr = longArr;125for (long l : arr) {126dummy += innerCandidate1long(l);127}128return dummy;129}130131private int innerCandidate2int(int i) {132int j;133134if (i < 5) {135/* j becomes 3, so it should be straightened to j == 3 case below. */136j = 3;137} else {138/* j becomes 4, so it should be straightened to j == 4 case below. */139j = 4;140}141142if (j == 4) {143i += 2;144}145if (j == 3) {146i += 4;147}148return i;149}150151/** Tests how well constant integer definitions are straightened. */152@Benchmark153public int testStraighten2int() throws Exception {154int[] arr = intArr;155int dummy = 0;156for (int i : arr) {157dummy += innerCandidate2int(i);158}159return dummy;160}161162private long innerCandidate2long(long l) {163long j;164165if (l < 5) {166/* j becomes 3, so it should be straightened to j == 3 case below. */167j = 3;168} else {169/* j becomes 4, so it should be straightened to j == 4 case below. */170j = 4;171}172173if (j == 4) {174l += 2;175}176if (j == 3) {177l += 4;178}179return l;180}181182/** Tests how well constant long definitions are straightened. */183@Benchmark184public int testStraighten2long() throws Exception {185int dummy = 0;186long[] arr = longArr;187for (long l : arr) {188dummy += innerCandidate2long(l);189}190return dummy;191}192193private int innerCandidate3int(int i, int j) {194int k = 0;195196if (i == j) {197k++;198}199if (i != j) {200k += 2;201}202if (i < j) {203k += 4;204}205return k;206}207208/**209* Tests how well variable integer compares are straightened.210*/211@Benchmark212public int testStraighten3int() throws Exception {213int dummy = 0;214int[] arr = intArr;215for (int i = 0; i < arr.length - 1; i++) {216dummy += innerCandidate3int(arr[i], arr[i + 1]);217}218return dummy;219}220221private long innerCandidate3long(long i, long j) {222long k = 0;223224if (i == j) {225k++;226}227if (i != j) {228k += 2;229}230if (i < j) {231k += 4;232}233return k;234}235236/** Tests how well variable long compares are straightened. */237@Benchmark238public int testStraighten3long() throws Exception {239int dummy = 0;240long[] arr = longArr;241for (int i = 0; i < arr.length - 1; i++) {242dummy += innerCandidate3long(arr[i], arr[i + 1]);243}244return dummy;245}246247}248249250