43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.lir.stackslotalloc;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.lir.LIRValueUtil.asVirtualStackSlot;
|
|
28 |
import static org.graalvm.compiler.lir.LIRValueUtil.isVirtualStackSlot;
|
|
29 |
import static org.graalvm.compiler.lir.stackslotalloc.StackSlotAllocatorUtil.allocatedFramesize;
|
|
30 |
import static org.graalvm.compiler.lir.stackslotalloc.StackSlotAllocatorUtil.allocatedSlots;
|
|
31 |
import static org.graalvm.compiler.lir.stackslotalloc.StackSlotAllocatorUtil.virtualFramesize;
|
|
32 |
|
|
33 |
import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
|
46640
|
34 |
import org.graalvm.compiler.debug.DebugContext;
|
|
35 |
import org.graalvm.compiler.debug.GraalError;
|
43972
|
36 |
import org.graalvm.compiler.debug.Indent;
|
|
37 |
import org.graalvm.compiler.lir.LIRInstruction;
|
|
38 |
import org.graalvm.compiler.lir.ValueProcedure;
|
|
39 |
import org.graalvm.compiler.lir.VirtualStackSlot;
|
|
40 |
import org.graalvm.compiler.lir.framemap.FrameMapBuilderTool;
|
|
41 |
import org.graalvm.compiler.lir.framemap.SimpleVirtualStackSlot;
|
|
42 |
import org.graalvm.compiler.lir.framemap.VirtualStackSlotRange;
|
|
43 |
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
|
|
44 |
import org.graalvm.compiler.lir.phases.AllocationPhase;
|
|
45 |
|
|
46 |
import jdk.vm.ci.code.StackSlot;
|
|
47 |
import jdk.vm.ci.code.TargetDescription;
|
|
48 |
|
|
49 |
public class SimpleStackSlotAllocator extends AllocationPhase {
|
|
50 |
|
|
51 |
@Override
|
|
52 |
protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) {
|
|
53 |
allocateStackSlots((FrameMapBuilderTool) lirGenRes.getFrameMapBuilder(), lirGenRes);
|
|
54 |
lirGenRes.buildFrameMap();
|
|
55 |
}
|
|
56 |
|
|
57 |
public void allocateStackSlots(FrameMapBuilderTool builder, LIRGenerationResult res) {
|
46640
|
58 |
DebugContext debug = res.getLIR().getDebug();
|
43972
|
59 |
StackSlot[] mapping = new StackSlot[builder.getNumberOfStackSlots()];
|
46640
|
60 |
boolean allocatedFramesizeEnabled = allocatedFramesize.isEnabled(debug);
|
|
61 |
long currentFrameSize = allocatedFramesizeEnabled ? builder.getFrameMap().currentFrameSize() : 0;
|
43972
|
62 |
for (VirtualStackSlot virtualSlot : builder.getStackSlots()) {
|
|
63 |
final StackSlot slot;
|
|
64 |
if (virtualSlot instanceof SimpleVirtualStackSlot) {
|
|
65 |
slot = mapSimpleVirtualStackSlot(builder, (SimpleVirtualStackSlot) virtualSlot);
|
46640
|
66 |
virtualFramesize.add(debug, builder.getFrameMap().spillSlotSize(virtualSlot.getValueKind()));
|
43972
|
67 |
} else if (virtualSlot instanceof VirtualStackSlotRange) {
|
|
68 |
VirtualStackSlotRange slotRange = (VirtualStackSlotRange) virtualSlot;
|
|
69 |
slot = mapVirtualStackSlotRange(builder, slotRange);
|
46640
|
70 |
virtualFramesize.add(debug, builder.getFrameMap().spillSlotRangeSize(slotRange.getSlots()));
|
43972
|
71 |
} else {
|
|
72 |
throw GraalError.shouldNotReachHere("Unknown VirtualStackSlot: " + virtualSlot);
|
|
73 |
}
|
46640
|
74 |
allocatedSlots.increment(debug);
|
43972
|
75 |
mapping[virtualSlot.getId()] = slot;
|
|
76 |
}
|
|
77 |
updateLIR(res, mapping);
|
46640
|
78 |
if (allocatedFramesizeEnabled) {
|
|
79 |
allocatedFramesize.add(debug, builder.getFrameMap().currentFrameSize() - currentFrameSize);
|
43972
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
@SuppressWarnings("try")
|
|
84 |
protected void updateLIR(LIRGenerationResult res, StackSlot[] mapping) {
|
46640
|
85 |
DebugContext debug = res.getLIR().getDebug();
|
|
86 |
try (DebugContext.Scope scope = debug.scope("StackSlotMappingLIR")) {
|
43972
|
87 |
ValueProcedure updateProc = (value, mode, flags) -> {
|
|
88 |
if (isVirtualStackSlot(value)) {
|
|
89 |
StackSlot stackSlot = mapping[asVirtualStackSlot(value).getId()];
|
46640
|
90 |
debug.log("map %s -> %s", value, stackSlot);
|
43972
|
91 |
return stackSlot;
|
|
92 |
}
|
|
93 |
return value;
|
|
94 |
};
|
|
95 |
for (AbstractBlockBase<?> block : res.getLIR().getControlFlowGraph().getBlocks()) {
|
46640
|
96 |
try (Indent indent0 = debug.logAndIndent("block: %s", block)) {
|
43972
|
97 |
for (LIRInstruction inst : res.getLIR().getLIRforBlock(block)) {
|
46640
|
98 |
try (Indent indent1 = debug.logAndIndent("Inst: %d: %s", inst.id(), inst)) {
|
43972
|
99 |
inst.forEachAlive(updateProc);
|
|
100 |
inst.forEachInput(updateProc);
|
|
101 |
inst.forEachOutput(updateProc);
|
|
102 |
inst.forEachTemp(updateProc);
|
|
103 |
inst.forEachState(updateProc);
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
protected StackSlot mapSimpleVirtualStackSlot(FrameMapBuilderTool builder, SimpleVirtualStackSlot virtualStackSlot) {
|
|
112 |
return builder.getFrameMap().allocateSpillSlot(virtualStackSlot.getValueKind());
|
|
113 |
}
|
|
114 |
|
|
115 |
protected StackSlot mapVirtualStackSlotRange(FrameMapBuilderTool builder, VirtualStackSlotRange virtualStackSlot) {
|
55509
|
116 |
return builder.getFrameMap().allocateStackSlots(virtualStackSlot.getSlots());
|
43972
|
117 |
}
|
|
118 |
}
|