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.alloc.trace; 24 25 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig; 26 import org.graalvm.compiler.core.common.alloc.Trace; 27 import org.graalvm.compiler.core.common.alloc.TraceBuilderResult; 28 import org.graalvm.compiler.debug.Debug; 29 import org.graalvm.compiler.debug.Debug.Scope; 30 import org.graalvm.compiler.debug.DebugCloseable; 31 import org.graalvm.compiler.debug.DebugCounter; 32 import org.graalvm.compiler.debug.DebugMemUseTracker; 33 import org.graalvm.compiler.debug.DebugTimer; 34 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 35 import org.graalvm.compiler.lir.gen.LIRGeneratorTool.MoveFactory; 36 import org.graalvm.compiler.lir.phases.LIRPhase; 37 import org.graalvm.compiler.lir.phases.LIRPhase.LIRPhaseStatistics; 38 39 import jdk.vm.ci.code.TargetDescription; 40 41 public abstract class TraceAllocationPhase<C extends TraceAllocationPhase.TraceAllocationContext> { 42 43 public static class TraceAllocationContext { 44 public final MoveFactory spillMoveFactory; 45 public final RegisterAllocationConfig registerAllocationConfig; 46 public final TraceBuilderResult resultTraces; 47 48 public TraceAllocationContext(MoveFactory spillMoveFactory, RegisterAllocationConfig registerAllocationConfig, TraceBuilderResult resultTraces) { 49 this.spillMoveFactory = spillMoveFactory; 50 this.registerAllocationConfig = registerAllocationConfig; 51 this.resultTraces = resultTraces; 52 } 53 } 54 55 /** 56 * Records time spent within {@link #apply}. 57 */ 58 private final DebugTimer timer; 59 60 /** 61 * Records memory usage within {@link #apply}. 62 */ 63 private final DebugMemUseTracker memUseTracker; 64 65 /** 66 * Records the number of traces allocated with this phase. 67 */ 68 private final DebugCounter allocatedTraces; 69 70 private static final class AllocationStatistics { 71 private final DebugCounter allocatedTraces; 72 73 private AllocationStatistics(Class<?> clazz) { 74 allocatedTraces = Debug.counter("TraceRA[%s]", clazz); 75 } 76 } 77 78 private static final ClassValue<AllocationStatistics> counterClassValue = new ClassValue<AllocationStatistics>() { 79 @Override 80 protected AllocationStatistics computeValue(Class<?> c) { 81 return new AllocationStatistics(c); 82 } 83 }; 84 85 public TraceAllocationPhase() { 86 LIRPhaseStatistics statistics = LIRPhase.statisticsClassValue.get(getClass()); 87 timer = statistics.timer; 88 memUseTracker = statistics.memUseTracker; 89 allocatedTraces = counterClassValue.get(getClass()).allocatedTraces; 90 } 91 92 public final CharSequence getName() { 93 return LIRPhase.createName(getClass()); 94 } 95 96 @Override 97 public final String toString() { 98 return getName().toString(); 99 } 100 101 public final void apply(TargetDescription target, LIRGenerationResult lirGenRes, Trace trace, C context) { 102 apply(target, lirGenRes, trace, context, true); 103 } 104 105 @SuppressWarnings("try") 106 public final void apply(TargetDescription target, LIRGenerationResult lirGenRes, Trace trace, C context, boolean dumpTrace) { 107 try (Scope s = Debug.scope(getName(), this)) { 108 try (DebugCloseable a = timer.start(); DebugCloseable c = memUseTracker.start()) { 109 if (dumpTrace && Debug.isDumpEnabled(TraceBuilderPhase.TRACE_DUMP_LEVEL + 1)) { 110 Debug.dump(TraceBuilderPhase.TRACE_DUMP_LEVEL + 1, trace, "%s before (Trace%s: %s)", getName(), trace.getId(), trace); 111 } 112 run(target, lirGenRes, trace, context); 113 allocatedTraces.increment(); 114 if (dumpTrace && Debug.isDumpEnabled(TraceBuilderPhase.TRACE_DUMP_LEVEL)) { 115 Debug.dump(TraceBuilderPhase.TRACE_DUMP_LEVEL, trace, "%s (Trace%s: %s)", getName(), trace.getId(), trace); 116 } 117 } 118 } catch (Throwable e) { 119 throw Debug.handle(e); 120 } 121 } 122 123 protected abstract void run(TargetDescription target, LIRGenerationResult lirGenRes, Trace trace, C context); 124 }