1 /*
   2  * Copyright (c) 2015, 2015, 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.graalvm.compiler.lir.phases;
  24 
  25 import java.util.regex.Pattern;
  26 
  27 import org.graalvm.compiler.debug.Debug;
  28 import org.graalvm.compiler.debug.Debug.Scope;
  29 import org.graalvm.compiler.debug.DebugCloseable;
  30 import org.graalvm.compiler.debug.DebugMemUseTracker;
  31 import org.graalvm.compiler.debug.DebugTimer;
  32 import org.graalvm.compiler.lir.LIR;
  33 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  34 import org.graalvm.compiler.options.Option;
  35 import org.graalvm.compiler.options.OptionKey;
  36 import org.graalvm.compiler.options.OptionType;
  37 
  38 import jdk.vm.ci.code.TargetDescription;
  39 
  40 /**
  41  * Base class for all {@link LIR low-level} phases. Subclasses should be stateless. There will be
  42  * one global instance for each phase that is shared for all compilations.
  43  */
  44 public abstract class LIRPhase<C> {
  45 
  46     public static class Options {
  47         // @formatter:off
  48         @Option(help = "Enable LIR level optimiztations.", type = OptionType.Debug)
  49         public static final OptionKey<Boolean> LIROptimization = new OptionKey<>(true);
  50         // @formatter:on
  51     }
  52 
  53     /**
  54      * Records time spent within {@link #apply}.
  55      */
  56     private final DebugTimer timer;
  57 
  58     /**
  59      * Records memory usage within {@link #apply}.
  60      */
  61     private final DebugMemUseTracker memUseTracker;
  62 
  63     public static final class LIRPhaseStatistics {
  64         /**
  65          * Records time spent within {@link #apply}.
  66          */
  67         public final DebugTimer timer;
  68 
  69         /**
  70          * Records memory usage within {@link #apply}.
  71          */
  72         public final DebugMemUseTracker memUseTracker;
  73 
  74         public LIRPhaseStatistics(Class<?> clazz) {
  75             timer = Debug.timer("LIRPhaseTime_%s", clazz);
  76             memUseTracker = Debug.memUseTracker("LIRPhaseMemUse_%s", clazz);
  77         }
  78     }
  79 
  80     public static final ClassValue<LIRPhaseStatistics> statisticsClassValue = new ClassValue<LIRPhaseStatistics>() {
  81         @Override
  82         protected LIRPhaseStatistics computeValue(Class<?> c) {
  83             return new LIRPhaseStatistics(c);
  84         }
  85     };
  86 
  87     public static LIRPhaseStatistics getLIRPhaseStatistics(Class<?> c) {
  88         return statisticsClassValue.get(c);
  89     }
  90 
  91     /** Lazy initialization to create pattern only when assertions are enabled. */
  92     static class NamePatternHolder {
  93         static final Pattern NAME_PATTERN = Pattern.compile("[A-Z][A-Za-z0-9]+");
  94     }
  95 
  96     private static boolean checkName(CharSequence name) {
  97         assert name == null || NamePatternHolder.NAME_PATTERN.matcher(name).matches() : "illegal phase name: " + name;
  98         return true;
  99     }
 100 
 101     public LIRPhase() {
 102         LIRPhaseStatistics statistics = getLIRPhaseStatistics(getClass());
 103         timer = statistics.timer;
 104         memUseTracker = statistics.memUseTracker;
 105     }
 106 
 107     public final void apply(TargetDescription target, LIRGenerationResult lirGenRes, C context) {
 108         apply(target, lirGenRes, context, true);
 109     }
 110 
 111     @SuppressWarnings("try")
 112     public final void apply(TargetDescription target, LIRGenerationResult lirGenRes, C context, boolean dumpLIR) {
 113         try (Scope s = Debug.scope(getName(), this)) {
 114             try (DebugCloseable a = timer.start(); DebugCloseable c = memUseTracker.start()) {
 115                 run(target, lirGenRes, context);
 116                 if (dumpLIR && Debug.isDumpEnabled(Debug.BASIC_LOG_LEVEL)) {
 117                     Debug.dump(Debug.BASIC_LOG_LEVEL, lirGenRes.getLIR(), "%s", getName());
 118                 }
 119             }
 120         } catch (Throwable e) {
 121             throw Debug.handle(e);
 122         }
 123     }
 124 
 125     protected abstract void run(TargetDescription target, LIRGenerationResult lirGenRes, C context);
 126 
 127     public static CharSequence createName(Class<?> clazz) {
 128         String className = clazz.getName();
 129         String s = className.substring(className.lastIndexOf(".") + 1); // strip the package name
 130         int innerClassPos = s.indexOf('$');
 131         if (innerClassPos > 0) {
 132             /* Remove inner class name. */
 133             s = s.substring(0, innerClassPos);
 134         }
 135         if (s.endsWith("Phase")) {
 136             s = s.substring(0, s.length() - "Phase".length());
 137         }
 138         return s;
 139     }
 140 
 141     protected CharSequence createName() {
 142         return createName(getClass());
 143     }
 144 
 145     public final CharSequence getName() {
 146         CharSequence name = createName();
 147         assert checkName(name);
 148         return name;
 149     }
 150 }