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.OptionType; 36 import org.graalvm.compiler.options.OptionValue; 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 OptionValue<Boolean> LIROptimization = new OptionValue<>(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 private 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 /** Lazy initialization to create pattern only when assertions are enabled. */ 88 static class NamePatternHolder { 89 static final Pattern NAME_PATTERN = Pattern.compile("[A-Z][A-Za-z0-9]+"); 90 } 91 92 private static boolean checkName(CharSequence name) { 93 assert name == null || NamePatternHolder.NAME_PATTERN.matcher(name).matches() : "illegal phase name: " + name; 94 return true; 95 } 96 97 public LIRPhase() { 98 LIRPhaseStatistics statistics = statisticsClassValue.get(getClass()); 99 timer = statistics.timer; 100 memUseTracker = statistics.memUseTracker; 101 } 102 103 public final void apply(TargetDescription target, LIRGenerationResult lirGenRes, C context) { 104 apply(target, lirGenRes, context, true); 105 } 106 107 @SuppressWarnings("try") 108 public final void apply(TargetDescription target, LIRGenerationResult lirGenRes, C context, boolean dumpLIR) { 109 try (Scope s = Debug.scope(getName(), this)) { 110 try (DebugCloseable a = timer.start(); DebugCloseable c = memUseTracker.start()) { 111 run(target, lirGenRes, context); 112 if (dumpLIR && Debug.isDumpEnabled(Debug.BASIC_LOG_LEVEL)) { 113 Debug.dump(Debug.BASIC_LOG_LEVEL, lirGenRes.getLIR(), "%s", getName()); 114 } 115 } 116 } catch (Throwable e) { 117 throw Debug.handle(e); 118 } | 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 } |