1 /*
2  * Copyright (c) 2016, 2018, 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;
26 
27 import java.util.ArrayList;
28 
29 import org.graalvm.compiler.core.common.LIRKind;
30 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
31 import org.graalvm.compiler.lir.LIR;
32 import org.graalvm.compiler.lir.LIRInsertionBuffer;
33 import org.graalvm.compiler.lir.LIRInstruction;
34 import org.graalvm.compiler.lir.StandardOp;
35 import org.graalvm.compiler.lir.Variable;
36 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
37 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
38 import org.graalvm.compiler.lir.phases.PreAllocationOptimizationPhase;
39 import org.graalvm.compiler.lir.util.RegisterMap;
40 
41 import jdk.vm.ci.code.Architecture;
42 import jdk.vm.ci.code.Register;
43 import jdk.vm.ci.code.RegisterArray;
44 import jdk.vm.ci.code.RegisterValue;
45 import jdk.vm.ci.code.TargetDescription;
46 import jdk.vm.ci.meta.PlatformKind;
47 
48 public class SaveCalleeSaveRegisters extends PreAllocationOptimizationPhase {
49 
50     @Override
run(TargetDescription target, LIRGenerationResult lirGenRes, PreAllocationOptimizationContext context)51     protected void run(TargetDescription target, LIRGenerationResult lirGenRes, PreAllocationOptimizationContext context) {
52         RegisterArray calleeSaveRegisters = lirGenRes.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, lirGenRes, 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(), lirGenRes, savedRegisters, block);
65             }
66         }
67     }
68 
saveAtEntry(LIR lir, LIRGeneratorTool lirGen, LIRGenerationResult lirGenRes, RegisterArray calleeSaveRegisters, Architecture arch)69     private static RegisterMap<Variable> saveAtEntry(LIR lir, LIRGeneratorTool lirGen, LIRGenerationResult lirGenRes, 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             save.setComment(lirGenRes, "SaveCalleeSavedRegisters: saveAtEntry");
87             saveMap.put(register, saveVariable);
88             savedRegisterValues[savedRegisterValueIndex++] = registerValue;
89         }
90         entry.addIncomingValues(savedRegisterValues);
91         buffer.finish();
92         return saveMap;
93     }
94 
restoreAtExit(LIR lir, LIRGeneratorTool.MoveFactory moveFactory, LIRGenerationResult lirGenRes, RegisterMap<Variable> calleeSaveRegisters, AbstractBlockBase<?> block)95     private static void restoreAtExit(LIR lir, LIRGeneratorTool.MoveFactory moveFactory, LIRGenerationResult lirGenRes, RegisterMap<Variable> calleeSaveRegisters, AbstractBlockBase<?> block) {
96         ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
97         int insertionIndex = instructions.size() - 1;
98         LIRInsertionBuffer buffer = new LIRInsertionBuffer();
99         buffer.init(instructions);
100         assert instructions.get(insertionIndex) instanceof StandardOp.BlockEndOp;
101         calleeSaveRegisters.forEach((Register register, Variable saved) -> {
102             LIRInstruction restore = moveFactory.createMove(register.asValue(saved.getValueKind()), saved);
103             buffer.append(insertionIndex, restore);
104             restore.setComment(lirGenRes, "SaveCalleeSavedRegisters: restoreAtExit");
105         });
106         buffer.finish();
107     }
108 }
109