package org.graalvm.compiler.lir.phases;
import java.util.regex.Pattern;
import org.graalvm.compiler.debug.Debug;
import org.graalvm.compiler.debug.Debug.Scope;
import org.graalvm.compiler.debug.DebugCloseable;
import org.graalvm.compiler.debug.DebugMemUseTracker;
import org.graalvm.compiler.debug.DebugTimer;
import org.graalvm.compiler.lir.LIR;
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
import org.graalvm.compiler.options.Option;
import org.graalvm.compiler.options.OptionType;
import org.graalvm.compiler.options.OptionValue;
import jdk.vm.ci.code.TargetDescription;
public abstract class LIRPhase<C> {
public static class Options {
@Option(help = "Enable LIR level optimiztations.", type = OptionType.Debug)
public static final OptionValue<Boolean> LIROptimization = new OptionValue<>(true);
}
private final DebugTimer timer;
private final DebugMemUseTracker memUseTracker;
public static final class LIRPhaseStatistics {
public final DebugTimer timer;
public final DebugMemUseTracker memUseTracker;
private LIRPhaseStatistics(Class<?> clazz) {
timer = Debug.timer("LIRPhaseTime_%s", clazz);
memUseTracker = Debug.memUseTracker("LIRPhaseMemUse_%s", clazz);
}
}
public static final ClassValue<LIRPhaseStatistics> statisticsClassValue = new ClassValue<LIRPhaseStatistics>() {
@Override
protected LIRPhaseStatistics computeValue(Class<?> c) {
return new LIRPhaseStatistics(c);
}
};
static class NamePatternHolder {
static final Pattern NAME_PATTERN = Pattern.compile("[A-Z][A-Za-z0-9]+");
}
private static boolean checkName(CharSequence name) {
assert name == null || NamePatternHolder.NAME_PATTERN.matcher(name).matches() : "illegal phase name: " + name;
return true;
}
public LIRPhase() {
LIRPhaseStatistics statistics = statisticsClassValue.get(getClass());
timer = statistics.timer;
memUseTracker = statistics.memUseTracker;
}
public final void apply(TargetDescription target, LIRGenerationResult lirGenRes, C context) {
apply(target, lirGenRes, context, true);
}
@SuppressWarnings("try")
public final void apply(TargetDescription target, LIRGenerationResult lirGenRes, C context, boolean dumpLIR) {
try (Scope s = Debug.scope(getName(), this)) {
try (DebugCloseable a = timer.start(); DebugCloseable c = memUseTracker.start()) {
run(target, lirGenRes, context);
if (dumpLIR && Debug.isDumpEnabled(Debug.BASIC_LOG_LEVEL)) {
Debug.dump(Debug.BASIC_LOG_LEVEL, lirGenRes.getLIR(), "%s", getName());
}
}
} catch (Throwable e) {
throw Debug.handle(e);
}
}
protected abstract void run(TargetDescription target, LIRGenerationResult lirGenRes, C context);
public static CharSequence createName(Class<?> clazz) {
String className = clazz.getName();
String s = className.substring(className.lastIndexOf(".") + 1);
int innerClassPos = s.indexOf('$');
if (innerClassPos > 0) {
s = s.substring(0, innerClassPos);
}
if (s.endsWith("Phase")) {
s = s.substring(0, s.length() - "Phase".length());
}
return s;
}
protected CharSequence createName() {
return createName(getClass());
}
public final CharSequence getName() {
CharSequence name = createName();
assert checkName(name);
return name;
}
}