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