1 /*
   2  * Copyright (c) 2015, 2019, 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 package org.graalvm.compiler.microbenchmarks.graal;
  26 
  27 import org.graalvm.compiler.microbenchmarks.graal.util.GraalState;
  28 import org.graalvm.compiler.microbenchmarks.graal.util.GraphState;
  29 import org.graalvm.compiler.microbenchmarks.graal.util.MethodSpec;
  30 import org.graalvm.compiler.phases.common.ConditionalEliminationPhase;
  31 import org.openjdk.jmh.annotations.Benchmark;
  32 
  33 public class ConditionalEliminationBenchmark extends GraalBenchmark {
  34 
  35     @MethodSpec(declaringClass = ConditionalEliminationBenchmark.class, name = "nullnessSnippet")
  36     public static class Nullness extends GraphState {
  37     }
  38 
  39     @SuppressWarnings("unused")
  40     public static int nullnessSnippet(Object a, Object b, int n, int m) {
  41         int result = 0;
  42         if (a == null) {
  43             if (a == b) {
  44                 if (b == null) {
  45                     for (int i = 0; i < n; ++i) {
  46                         if (i % 3 == 0) {
  47                             break;
  48                         }
  49                         result <<= 1;
  50                         if (i % 7 == 0) {
  51                             break;
  52                         }
  53                     }
  54                     return 1;
  55                 } else {
  56                     return -2;
  57                 }
  58             } else {
  59                 if (b == null) {
  60                     for (int i = 0; i < n; ++i) {
  61                         if (i % 4 == 0) {
  62                             break;
  63                         }
  64                         result <<= 1;
  65                         if (i % 7 == 0) {
  66                             break;
  67                         }
  68                     }
  69                     return result;
  70                 } else {
  71                     return 4;
  72                 }
  73             }
  74         } else {
  75             if (a == b) {
  76                 if (b == null) {
  77                     for (int i = 0; i < n; ++i) {
  78                         if (i % 5 == 0) {
  79                             break;
  80                         }
  81                         result <<= 1;
  82                         if (i % 7 == 0) {
  83                             break;
  84                         }
  85                     }
  86                     return result;
  87                 } else {
  88                     return 6;
  89                 }
  90             } else {
  91                 if (b == null) {
  92                     for (int i = 0; i < n; ++i) {
  93                         if (i % 6 == 0) {
  94                             break;
  95                         }
  96                         result <<= 1;
  97                         if (i % 7 == 0) {
  98                             break;
  99                         }
 100                     }
 101                     return result;
 102                 } else {
 103                     return 8;
 104                 }
 105             }
 106         }
 107     }
 108 
 109     @Benchmark
 110     public void nullness(Nullness s, GraalState g) {
 111         new ConditionalEliminationPhase(false).apply(s.graph, g.providers);
 112     }
 113 
 114     @Benchmark
 115     public void newDominatorConditionalElimination(Nullness s, GraalState g) {
 116         new ConditionalEliminationPhase(false).apply(s.graph, g.providers);
 117     }
 118 
 119     @MethodSpec(declaringClass = ConditionalEliminationBenchmark.class, name = "searchSnippet")
 120     public static class Search extends GraphState {
 121     }
 122 
 123     static class Entry {
 124         final String name;
 125 
 126         Entry(String name) {
 127             this.name = name;
 128         }
 129     }
 130 
 131     static class EntryWithNext extends Entry {
 132         EntryWithNext(String name, Entry next) {
 133             super(name);
 134             this.next = next;
 135         }
 136 
 137         final Entry next;
 138     }
 139 
 140     public static Entry searchSnippet(Entry start, String name, Entry alternative) {
 141         Entry current = start;
 142         do {
 143             while (current instanceof EntryWithNext) {
 144                 if (name != null && current.name == name) {
 145                     current = null;
 146                 } else {
 147                     Entry next = ((EntryWithNext) current).next;
 148                     current = next;
 149                 }
 150             }
 151 
 152             if (current != null) {
 153                 if (current.name.equals(name)) {
 154                     return current;
 155                 }
 156             }
 157             if (current == alternative) {
 158                 return null;
 159             }
 160             current = alternative;
 161 
 162         } while (true);
 163     }
 164 
 165     @Benchmark
 166     public void search(Search s, GraalState g) {
 167         new ConditionalEliminationPhase(false).apply(s.graph, g.providers);
 168     }
 169 }