1 /* 2 * Copyright (c) 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.hotspot.lir; 24 25 import static jdk.vm.ci.code.ValueUtil.isStackSlot; 26 27 import java.util.ArrayList; 28 29 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 30 import org.graalvm.compiler.debug.Debug; 31 import org.graalvm.compiler.debug.Indent; 32 import org.graalvm.compiler.hotspot.HotSpotLIRGenerationResult; 33 import org.graalvm.compiler.hotspot.stubs.Stub; 34 import org.graalvm.compiler.lir.LIR; 35 import org.graalvm.compiler.lir.LIRFrameState; 36 import org.graalvm.compiler.lir.LIRInsertionBuffer; 37 import org.graalvm.compiler.lir.LIRInstruction; 38 import org.graalvm.compiler.lir.StandardOp.SaveRegistersOp; 39 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool; 40 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool.ZapRegistersAfterInstruction; 41 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool.ZapStackArgumentSpaceBeforeInstruction; 42 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 43 import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase; 44 45 import jdk.vm.ci.code.TargetDescription; 46 import jdk.vm.ci.meta.AllocatableValue; 47 48 /** 49 * Inserts a {@link DiagnosticLIRGeneratorTool#createZapRegisters ZapRegistersOp} after 50 * {@link ZapRegistersAfterInstruction} for stubs and 51 * {@link DiagnosticLIRGeneratorTool#zapArgumentSpace ZapArgumentSpaceOp} after 52 * {@link ZapStackArgumentSpaceBeforeInstruction} for all compiles. 53 */ 54 public final class HotSpotZapRegistersPhase extends PostAllocationOptimizationPhase { 55 56 @Override 57 protected void run(TargetDescription target, LIRGenerationResult lirGenRes, PostAllocationOptimizationContext context) { 58 Stub stub = ((HotSpotLIRGenerationResult) lirGenRes).getStub(); 59 boolean zapRegisters = stub != null && !stub.preservesRegisters(); 60 boolean zapStack = false; 61 for (AllocatableValue arg : lirGenRes.getCallingConvention().getArguments()) { 62 if (isStackSlot(arg)) { 63 zapStack = true; 64 break; 65 } 66 } 67 if (zapRegisters || zapStack) { 68 LIR lir = lirGenRes.getLIR(); 69 processLIR(context.diagnosticLirGenTool, (HotSpotLIRGenerationResult) lirGenRes, lir, zapRegisters, zapStack); 70 } 71 } 72 73 private static void processLIR(DiagnosticLIRGeneratorTool diagnosticLirGenTool, HotSpotLIRGenerationResult res, LIR lir, boolean zapRegisters, boolean zapStack) { 74 LIRInsertionBuffer buffer = new LIRInsertionBuffer(); 75 for (AbstractBlockBase<?> block : lir.codeEmittingOrder()) { 76 if (block != null) { 77 processBlock(diagnosticLirGenTool, res, lir, buffer, block, zapRegisters, zapStack); 78 } 79 } 80 } 81 82 @SuppressWarnings("try") 83 private static void processBlock(DiagnosticLIRGeneratorTool diagnosticLirGenTool, HotSpotLIRGenerationResult res, LIR lir, LIRInsertionBuffer buffer, AbstractBlockBase<?> block, 84 boolean zapRegisters, boolean zapStack) { 85 try (Indent indent = Debug.logAndIndent("Process block %s", block)) { 86 ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block); 87 buffer.init(instructions); 88 for (int index = 0; index < instructions.size(); index++) { 89 LIRInstruction inst = instructions.get(index); 90 if (zapStack && inst instanceof ZapStackArgumentSpaceBeforeInstruction) { 91 LIRInstruction zap = diagnosticLirGenTool.zapArgumentSpace(); 92 if (zap != null) { 93 buffer.append(index, zap); 94 } 95 } 96 if (zapRegisters && inst instanceof ZapRegistersAfterInstruction) { 97 LIRFrameState state = getLIRState(inst); 98 if (state != null) { 99 SaveRegistersOp zap = diagnosticLirGenTool.createZapRegisters(); 100 SaveRegistersOp old = res.getCalleeSaveInfo().put(state, zap); 101 assert old == null : "Already another SaveRegisterOp registered! " + old; 102 buffer.append(index + 1, (LIRInstruction) zap); 103 Debug.log("Insert ZapRegister after %s", inst); 104 } 105 } 106 } 107 buffer.finish(); 108 } 109 } 110 111 /** 112 * Returns the {@link LIRFrameState} of an instruction. 113 */ 114 private static LIRFrameState getLIRState(LIRInstruction inst) { 115 final LIRFrameState[] lirState = {null}; 116 inst.forEachState(state -> { 117 assert lirState[0] == null : "Multiple states: " + inst; 118 lirState[0] = state; 119 }); 120 assert lirState[0] != null : "No state: " + inst; 121 return lirState[0]; 122 } 123 124 }