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.DebugContext;
  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         DebugContext debug = lir.getDebug();
  86         try (Indent indent = debug.logAndIndent("Process block %s", block)) {
  87             ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
  88             buffer.init(instructions);
  89             for (int index = 0; index < instructions.size(); index++) {
  90                 LIRInstruction inst = instructions.get(index);
  91                 if (zapStack && inst instanceof ZapStackArgumentSpaceBeforeInstruction) {
  92                     LIRInstruction zap = diagnosticLirGenTool.zapArgumentSpace();
  93                     if (zap != null) {
  94                         buffer.append(index, zap);
  95                     }
  96                 }
  97                 if (zapRegisters && inst instanceof ZapRegistersAfterInstruction) {
  98                     LIRFrameState state = getLIRState(inst);
  99                     if (state != null) {
 100                         SaveRegistersOp zap = diagnosticLirGenTool.createZapRegisters();
 101                         SaveRegistersOp old = res.getCalleeSaveInfo().put(state, zap);
 102                         assert old == null : "Already another SaveRegisterOp registered! " + old;
 103                         buffer.append(index + 1, (LIRInstruction) zap);
 104                         debug.log("Insert ZapRegister after %s", inst);
 105                     }
 106                 }
 107             }
 108             buffer.finish();
 109         }
 110     }
 111 
 112     /**
 113      * Returns the {@link LIRFrameState} of an instruction.
 114      */
 115     private static LIRFrameState getLIRState(LIRInstruction inst) {
 116         final LIRFrameState[] lirState = {null};
 117         inst.forEachState(state -> {
 118             assert lirState[0] == null : "Multiple states: " + inst;
 119             lirState[0] = state;
 120         });
 121         assert lirState[0] != null : "No state: " + inst;
 122         return lirState[0];
 123     }
 124 
 125 }