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;
24
25 import java.util.List;
26
27 import org.graalvm.compiler.core.common.LIRKind;
28 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
29 import org.graalvm.compiler.lir.LIR;
30 import org.graalvm.compiler.lir.LIRInsertionBuffer;
31 import org.graalvm.compiler.lir.LIRInstruction;
32 import org.graalvm.compiler.lir.StandardOp;
33 import org.graalvm.compiler.lir.Variable;
34 import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
35 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
36 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
37 import org.graalvm.compiler.lir.phases.PreAllocationOptimizationPhase;
38 import org.graalvm.compiler.lir.util.RegisterMap;
39
40 import jdk.vm.ci.code.Architecture;
41 import jdk.vm.ci.code.Register;
42 import jdk.vm.ci.code.RegisterArray;
43 import jdk.vm.ci.code.RegisterValue;
44 import jdk.vm.ci.code.TargetDescription;
45 import jdk.vm.ci.meta.PlatformKind;
51 FrameMapBuilder frameMapBuilder = lirGenRes.getFrameMapBuilder();
52 RegisterArray calleeSaveRegisters = frameMapBuilder.getCodeCache().getRegisterConfig().getCalleeSaveRegisters();
53 if (calleeSaveRegisters == null || calleeSaveRegisters.size() == 0) {
54 return;
55 }
56 LIR lir = lirGenRes.getLIR();
57 RegisterMap<Variable> savedRegisters = saveAtEntry(lir, context.lirGen, calleeSaveRegisters, target.arch);
58
59 for (AbstractBlockBase<?> block : lir.codeEmittingOrder()) {
60 if (block == null) {
61 continue;
62 }
63 if (block.getSuccessorCount() == 0) {
64 restoreAtExit(lir, context.lirGen.getSpillMoveFactory(), savedRegisters, block);
65 }
66 }
67 }
68
69 private static RegisterMap<Variable> saveAtEntry(LIR lir, LIRGeneratorTool lirGen, RegisterArray calleeSaveRegisters, Architecture arch) {
70 AbstractBlockBase<?> startBlock = lir.getControlFlowGraph().getStartBlock();
71 List<LIRInstruction> instructions = lir.getLIRforBlock(startBlock);
72 int insertionIndex = 1;
73 LIRInsertionBuffer buffer = new LIRInsertionBuffer();
74 buffer.init(instructions);
75 StandardOp.LabelOp entry = (StandardOp.LabelOp) instructions.get(insertionIndex - 1);
76 RegisterValue[] savedRegisterValues = new RegisterValue[calleeSaveRegisters.size()];
77 int savedRegisterValueIndex = 0;
78 RegisterMap<Variable> saveMap = new RegisterMap<>(arch);
79 for (Register register : calleeSaveRegisters) {
80 PlatformKind registerPlatformKind = arch.getLargestStorableKind(register.getRegisterCategory());
81 LIRKind lirKind = LIRKind.value(registerPlatformKind);
82 RegisterValue registerValue = register.asValue(lirKind);
83 Variable saveVariable = lirGen.newVariable(lirKind);
84 LIRInstruction save = lirGen.getSpillMoveFactory().createMove(saveVariable, registerValue);
85 buffer.append(insertionIndex, save);
86 saveMap.put(register, saveVariable);
87 savedRegisterValues[savedRegisterValueIndex++] = registerValue;
88 }
89 entry.addIncomingValues(savedRegisterValues);
90 buffer.finish();
91 return saveMap;
92 }
93
94 private static void restoreAtExit(LIR lir, LIRGeneratorTool.MoveFactory moveFactory, RegisterMap<Variable> calleeSaveRegisters, AbstractBlockBase<?> block) {
95 List<LIRInstruction> instructions = lir.getLIRforBlock(block);
96 int insertionIndex = instructions.size() - 1;
97 LIRInsertionBuffer buffer = new LIRInsertionBuffer();
98 buffer.init(instructions);
99 assert instructions.get(insertionIndex) instanceof StandardOp.BlockEndOp;
100 calleeSaveRegisters.forEach((Register register, Variable saved) -> {
101 LIRInstruction restore = moveFactory.createMove(register.asValue(saved.getValueKind()), saved);
102 buffer.append(insertionIndex, restore);
103 });
104 buffer.finish();
105 }
106 }
|
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;
24
25 import java.util.ArrayList;
26
27 import org.graalvm.compiler.core.common.LIRKind;
28 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
29 import org.graalvm.compiler.lir.LIR;
30 import org.graalvm.compiler.lir.LIRInsertionBuffer;
31 import org.graalvm.compiler.lir.LIRInstruction;
32 import org.graalvm.compiler.lir.StandardOp;
33 import org.graalvm.compiler.lir.Variable;
34 import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
35 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
36 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
37 import org.graalvm.compiler.lir.phases.PreAllocationOptimizationPhase;
38 import org.graalvm.compiler.lir.util.RegisterMap;
39
40 import jdk.vm.ci.code.Architecture;
41 import jdk.vm.ci.code.Register;
42 import jdk.vm.ci.code.RegisterArray;
43 import jdk.vm.ci.code.RegisterValue;
44 import jdk.vm.ci.code.TargetDescription;
45 import jdk.vm.ci.meta.PlatformKind;
51 FrameMapBuilder frameMapBuilder = lirGenRes.getFrameMapBuilder();
52 RegisterArray calleeSaveRegisters = frameMapBuilder.getCodeCache().getRegisterConfig().getCalleeSaveRegisters();
53 if (calleeSaveRegisters == null || calleeSaveRegisters.size() == 0) {
54 return;
55 }
56 LIR lir = lirGenRes.getLIR();
57 RegisterMap<Variable> savedRegisters = saveAtEntry(lir, context.lirGen, calleeSaveRegisters, target.arch);
58
59 for (AbstractBlockBase<?> block : lir.codeEmittingOrder()) {
60 if (block == null) {
61 continue;
62 }
63 if (block.getSuccessorCount() == 0) {
64 restoreAtExit(lir, context.lirGen.getSpillMoveFactory(), savedRegisters, block);
65 }
66 }
67 }
68
69 private static RegisterMap<Variable> saveAtEntry(LIR lir, LIRGeneratorTool lirGen, RegisterArray calleeSaveRegisters, Architecture arch) {
70 AbstractBlockBase<?> startBlock = lir.getControlFlowGraph().getStartBlock();
71 ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(startBlock);
72 int insertionIndex = 1;
73 LIRInsertionBuffer buffer = new LIRInsertionBuffer();
74 buffer.init(instructions);
75 StandardOp.LabelOp entry = (StandardOp.LabelOp) instructions.get(insertionIndex - 1);
76 RegisterValue[] savedRegisterValues = new RegisterValue[calleeSaveRegisters.size()];
77 int savedRegisterValueIndex = 0;
78 RegisterMap<Variable> saveMap = new RegisterMap<>(arch);
79 for (Register register : calleeSaveRegisters) {
80 PlatformKind registerPlatformKind = arch.getLargestStorableKind(register.getRegisterCategory());
81 LIRKind lirKind = LIRKind.value(registerPlatformKind);
82 RegisterValue registerValue = register.asValue(lirKind);
83 Variable saveVariable = lirGen.newVariable(lirKind);
84 LIRInstruction save = lirGen.getSpillMoveFactory().createMove(saveVariable, registerValue);
85 buffer.append(insertionIndex, save);
86 saveMap.put(register, saveVariable);
87 savedRegisterValues[savedRegisterValueIndex++] = registerValue;
88 }
89 entry.addIncomingValues(savedRegisterValues);
90 buffer.finish();
91 return saveMap;
92 }
93
94 private static void restoreAtExit(LIR lir, LIRGeneratorTool.MoveFactory moveFactory, RegisterMap<Variable> calleeSaveRegisters, AbstractBlockBase<?> block) {
95 ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
96 int insertionIndex = instructions.size() - 1;
97 LIRInsertionBuffer buffer = new LIRInsertionBuffer();
98 buffer.init(instructions);
99 assert instructions.get(insertionIndex) instanceof StandardOp.BlockEndOp;
100 calleeSaveRegisters.forEach((Register register, Variable saved) -> {
101 LIRInstruction restore = moveFactory.createMove(register.asValue(saved.getValueKind()), saved);
102 buffer.append(insertionIndex, restore);
103 });
104 buffer.finish();
105 }
106 }
|