1 /*
   2  * Copyright (c) 2016, 2019, 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.hotspot.lir;
  26 
  27 import static jdk.vm.ci.code.ValueUtil.isStackSlot;
  28 
  29 import java.util.ArrayList;
  30 
  31 import jdk.internal.vm.compiler.collections.EconomicSet;
  32 import jdk.internal.vm.compiler.collections.Equivalence;
  33 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  34 import org.graalvm.compiler.debug.DebugContext;
  35 import org.graalvm.compiler.debug.Indent;
  36 import org.graalvm.compiler.hotspot.HotSpotLIRGenerationResult;
  37 import org.graalvm.compiler.hotspot.stubs.Stub;
  38 import org.graalvm.compiler.lir.LIR;
  39 import org.graalvm.compiler.lir.LIRInsertionBuffer;
  40 import org.graalvm.compiler.lir.LIRInstruction;
  41 import org.graalvm.compiler.lir.StandardOp.ZapRegistersOp;
  42 import org.graalvm.compiler.lir.ValueConsumer;
  43 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool;
  44 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool.ZapRegistersAfterInstruction;
  45 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool.ZapStackArgumentSpaceBeforeInstruction;
  46 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  47 import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase;
  48 
  49 import jdk.vm.ci.code.CallingConvention;
  50 import jdk.vm.ci.code.Register;
  51 import jdk.vm.ci.code.TargetDescription;
  52 import jdk.vm.ci.code.ValueUtil;
  53 import jdk.vm.ci.meta.AllocatableValue;
  54 
  55 /**
  56  * Inserts a {@link DiagnosticLIRGeneratorTool#createZapRegisters ZapRegistersOp} after
  57  * {@link ZapRegistersAfterInstruction} for stubs and
  58  * {@link DiagnosticLIRGeneratorTool#zapArgumentSpace ZapArgumentSpaceOp} after
  59  * {@link ZapStackArgumentSpaceBeforeInstruction} for all compiles.
  60  */
  61 public final class HotSpotZapRegistersPhase extends PostAllocationOptimizationPhase {
  62 
  63     @Override
  64     protected void run(TargetDescription target, LIRGenerationResult lirGenRes, PostAllocationOptimizationContext context) {
  65         Stub stub = ((HotSpotLIRGenerationResult) lirGenRes).getStub();
  66         boolean zapRegisters = stub == null;
  67         boolean zapStack = false;
  68         CallingConvention callingConvention = lirGenRes.getCallingConvention();
  69         for (AllocatableValue arg : callingConvention.getArguments()) {
  70             if (isStackSlot(arg)) {
  71                 zapStack = true;
  72                 break;
  73             }
  74         }
  75         if (zapRegisters || zapStack) {
  76             LIR lir = lirGenRes.getLIR();
  77             EconomicSet<Register> allocatableRegisters = EconomicSet.create(Equivalence.IDENTITY);
  78             for (Register r : lirGenRes.getFrameMap().getRegisterConfig().getAllocatableRegisters()) {
  79                 allocatableRegisters.add(r);
  80             }
  81             processLIR(context.diagnosticLirGenTool, lir, allocatableRegisters, zapRegisters, zapStack);
  82         }
  83     }
  84 
  85     private static void processLIR(DiagnosticLIRGeneratorTool diagnosticLirGenTool, LIR lir, EconomicSet<Register> allocatableRegisters, boolean zapRegisters, boolean zapStack) {
  86         LIRInsertionBuffer buffer = new LIRInsertionBuffer();
  87         for (AbstractBlockBase<?> block : lir.codeEmittingOrder()) {
  88             if (block != null) {
  89                 processBlock(diagnosticLirGenTool, lir, allocatableRegisters, buffer, block, zapRegisters, zapStack);
  90             }
  91         }
  92     }
  93 
  94     @SuppressWarnings("try")
  95     private static void processBlock(DiagnosticLIRGeneratorTool diagnosticLirGenTool, LIR lir, EconomicSet<Register> allocatableRegisters, LIRInsertionBuffer buffer, AbstractBlockBase<?> block,
  96                     boolean zapRegisters, boolean zapStack) {
  97         DebugContext debug = lir.getDebug();
  98         try (Indent indent = debug.logAndIndent("Process block %s", block)) {
  99             ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
 100             buffer.init(instructions);
 101             for (int index = 0; index < instructions.size(); index++) {
 102                 LIRInstruction inst = instructions.get(index);
 103                 if (zapStack && inst instanceof ZapStackArgumentSpaceBeforeInstruction) {
 104                     LIRInstruction zap = diagnosticLirGenTool.zapArgumentSpace();
 105                     if (zap != null) {
 106                         buffer.append(index, zap);
 107                     }
 108                 }
 109                 if (zapRegisters && inst instanceof ZapRegistersAfterInstruction) {
 110                     final EconomicSet<Register> destroyedRegisters = EconomicSet.create(Equivalence.IDENTITY);
 111                     ValueConsumer tempConsumer = (value, mode, flags) -> {
 112                         if (ValueUtil.isRegister(value)) {
 113                             final Register reg = ValueUtil.asRegister(value);
 114                             if (allocatableRegisters.contains(reg)) {
 115                                 destroyedRegisters.add(reg);
 116                             }
 117                         }
 118                     };
 119                     ValueConsumer defConsumer = (value, mode, flags) -> {
 120                         if (ValueUtil.isRegister(value)) {
 121                             final Register reg = ValueUtil.asRegister(value);
 122                             destroyedRegisters.remove(reg);
 123                         }
 124                     };
 125                     inst.visitEachTemp(tempConsumer);
 126                     inst.visitEachOutput(defConsumer);
 127 
 128                     ZapRegistersOp zap = diagnosticLirGenTool.createZapRegisters(destroyedRegisters.toArray(new Register[destroyedRegisters.size()]));
 129                     buffer.append(index + 1, (LIRInstruction) zap);
 130                     debug.log("Insert ZapRegister after %s", inst);
 131                 }
 132             }
 133             buffer.finish();
 134         }
 135     }
 136 }