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