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.framemap;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.lir.LIRValueUtil.isVirtualStackSlot;
|
|
28 |
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.EnumSet;
|
|
31 |
import java.util.List;
|
|
32 |
|
|
33 |
import org.graalvm.compiler.core.common.LIRKind;
|
|
34 |
import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
|
46640
|
35 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
36 |
import org.graalvm.compiler.lir.InstructionValueConsumer;
|
|
37 |
import org.graalvm.compiler.lir.LIR;
|
|
38 |
import org.graalvm.compiler.lir.LIRInstruction;
|
|
39 |
import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
|
|
40 |
import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
|
|
41 |
import org.graalvm.compiler.lir.VirtualStackSlot;
|
|
42 |
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
|
|
43 |
|
|
44 |
import jdk.vm.ci.code.CallingConvention;
|
|
45 |
import jdk.vm.ci.code.CodeCacheProvider;
|
|
46 |
import jdk.vm.ci.code.RegisterConfig;
|
|
47 |
import jdk.vm.ci.meta.Value;
|
|
48 |
import jdk.vm.ci.meta.ValueKind;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* A FrameMapBuilder that records allocation.
|
|
52 |
*/
|
|
53 |
public class FrameMapBuilderImpl extends FrameMapBuilderTool {
|
|
54 |
|
|
55 |
private final RegisterConfig registerConfig;
|
|
56 |
private final CodeCacheProvider codeCache;
|
|
57 |
private final FrameMap frameMap;
|
|
58 |
private final List<VirtualStackSlot> stackSlots;
|
|
59 |
private final List<CallingConvention> calls;
|
|
60 |
private int numStackSlots;
|
|
61 |
|
|
62 |
public FrameMapBuilderImpl(FrameMap frameMap, CodeCacheProvider codeCache, RegisterConfig registerConfig) {
|
|
63 |
assert registerConfig != null : "No register config!";
|
|
64 |
this.registerConfig = registerConfig == null ? codeCache.getRegisterConfig() : registerConfig;
|
|
65 |
this.codeCache = codeCache;
|
|
66 |
this.frameMap = frameMap;
|
|
67 |
this.stackSlots = new ArrayList<>();
|
|
68 |
this.calls = new ArrayList<>();
|
|
69 |
this.numStackSlots = 0;
|
|
70 |
}
|
|
71 |
|
|
72 |
@Override
|
|
73 |
public VirtualStackSlot allocateSpillSlot(ValueKind<?> kind) {
|
|
74 |
SimpleVirtualStackSlot slot = new SimpleVirtualStackSlot(numStackSlots++, kind);
|
|
75 |
stackSlots.add(slot);
|
|
76 |
return slot;
|
|
77 |
}
|
|
78 |
|
|
79 |
@Override
|
55509
|
80 |
public VirtualStackSlot allocateStackSlots(int slots) {
|
43972
|
81 |
if (slots == 0) {
|
|
82 |
return null;
|
|
83 |
}
|
55509
|
84 |
VirtualStackSlotRange slot = new VirtualStackSlotRange(numStackSlots++, slots, LIRKind.value(frameMap.getTarget().arch.getWordKind()));
|
43972
|
85 |
stackSlots.add(slot);
|
|
86 |
return slot;
|
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
|
90 |
public RegisterConfig getRegisterConfig() {
|
|
91 |
return registerConfig;
|
|
92 |
}
|
|
93 |
|
|
94 |
@Override
|
|
95 |
public CodeCacheProvider getCodeCache() {
|
|
96 |
return codeCache;
|
|
97 |
}
|
|
98 |
|
|
99 |
@Override
|
|
100 |
public FrameMap getFrameMap() {
|
|
101 |
return frameMap;
|
|
102 |
}
|
|
103 |
|
|
104 |
@Override
|
|
105 |
public int getNumberOfStackSlots() {
|
|
106 |
return numStackSlots;
|
|
107 |
}
|
|
108 |
|
|
109 |
@Override
|
|
110 |
public void callsMethod(CallingConvention cc) {
|
|
111 |
calls.add(cc);
|
|
112 |
}
|
|
113 |
|
|
114 |
@Override
|
|
115 |
@SuppressWarnings("try")
|
|
116 |
public FrameMap buildFrameMap(LIRGenerationResult res) {
|
46640
|
117 |
DebugContext debug = res.getLIR().getDebug();
|
|
118 |
if (debug.areScopesEnabled()) {
|
43972
|
119 |
verifyStackSlotAllocation(res);
|
|
120 |
}
|
|
121 |
for (CallingConvention cc : calls) {
|
|
122 |
frameMap.callsMethod(cc);
|
|
123 |
}
|
|
124 |
frameMap.finish();
|
|
125 |
return frameMap;
|
|
126 |
}
|
|
127 |
|
|
128 |
private static void verifyStackSlotAllocation(LIRGenerationResult res) {
|
|
129 |
LIR lir = res.getLIR();
|
|
130 |
InstructionValueConsumer verifySlots = (LIRInstruction op, Value value, OperandMode mode, EnumSet<OperandFlag> flags) -> {
|
|
131 |
assert !isVirtualStackSlot(value) : String.format("Instruction %s contains a virtual stack slot %s", op, value);
|
|
132 |
};
|
|
133 |
for (AbstractBlockBase<?> block : lir.getControlFlowGraph().getBlocks()) {
|
|
134 |
lir.getLIRforBlock(block).forEach(op -> {
|
|
135 |
op.visitEachInput(verifySlots);
|
|
136 |
op.visitEachAlive(verifySlots);
|
|
137 |
op.visitEachState(verifySlots);
|
|
138 |
|
|
139 |
op.visitEachTemp(verifySlots);
|
|
140 |
op.visitEachOutput(verifySlots);
|
|
141 |
});
|
|
142 |
}
|
|
143 |
}
|
|
144 |
|
|
145 |
@Override
|
|
146 |
public List<VirtualStackSlot> getStackSlots() {
|
|
147 |
return stackSlots;
|
|
148 |
}
|
|
149 |
|
|
150 |
}
|