< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot/src/org/graalvm/compiler/hotspot/lir/HotSpotZapRegistersPhase.java

Print this page
rev 56282 : [mq]: graal
   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.hotspot.lir;
  26 
  27 import static jdk.vm.ci.code.ValueUtil.isStackSlot;
  28 
  29 import java.util.ArrayList;
  30 


  31 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  32 import org.graalvm.compiler.debug.DebugContext;
  33 import org.graalvm.compiler.debug.Indent;
  34 import org.graalvm.compiler.hotspot.HotSpotLIRGenerationResult;
  35 import org.graalvm.compiler.hotspot.stubs.Stub;
  36 import org.graalvm.compiler.lir.LIR;
  37 import org.graalvm.compiler.lir.LIRFrameState;
  38 import org.graalvm.compiler.lir.LIRInsertionBuffer;
  39 import org.graalvm.compiler.lir.LIRInstruction;
  40 import org.graalvm.compiler.lir.StandardOp.SaveRegistersOp;

  41 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool;
  42 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool.ZapRegistersAfterInstruction;
  43 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool.ZapStackArgumentSpaceBeforeInstruction;
  44 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  45 import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase;
  46 


  47 import jdk.vm.ci.code.TargetDescription;

  48 import jdk.vm.ci.meta.AllocatableValue;
  49 
  50 /**
  51  * Inserts a {@link DiagnosticLIRGeneratorTool#createZapRegisters ZapRegistersOp} after
  52  * {@link ZapRegistersAfterInstruction} for stubs and
  53  * {@link DiagnosticLIRGeneratorTool#zapArgumentSpace ZapArgumentSpaceOp} after
  54  * {@link ZapStackArgumentSpaceBeforeInstruction} for all compiles.
  55  */
  56 public final class HotSpotZapRegistersPhase extends PostAllocationOptimizationPhase {
  57 
  58     @Override
  59     protected void run(TargetDescription target, LIRGenerationResult lirGenRes, PostAllocationOptimizationContext context) {
  60         Stub stub = ((HotSpotLIRGenerationResult) lirGenRes).getStub();
  61         boolean zapRegisters = stub != null && !stub.preservesRegisters();
  62         boolean zapStack = false;
  63         for (AllocatableValue arg : lirGenRes.getCallingConvention().getArguments()) {

  64             if (isStackSlot(arg)) {
  65                 zapStack = true;
  66                 break;
  67             }
  68         }
  69         if (zapRegisters || zapStack) {
  70             LIR lir = lirGenRes.getLIR();
  71             processLIR(context.diagnosticLirGenTool, (HotSpotLIRGenerationResult) lirGenRes, lir, zapRegisters, zapStack);




  72         }
  73     }
  74 
  75     private static void processLIR(DiagnosticLIRGeneratorTool diagnosticLirGenTool, HotSpotLIRGenerationResult res, LIR lir, boolean zapRegisters, boolean zapStack) {
  76         LIRInsertionBuffer buffer = new LIRInsertionBuffer();
  77         for (AbstractBlockBase<?> block : lir.codeEmittingOrder()) {
  78             if (block != null) {
  79                 processBlock(diagnosticLirGenTool, res, lir, buffer, block, zapRegisters, zapStack);
  80             }
  81         }
  82     }
  83 
  84     @SuppressWarnings("try")
  85     private static void processBlock(DiagnosticLIRGeneratorTool diagnosticLirGenTool, HotSpotLIRGenerationResult res, LIR lir, LIRInsertionBuffer buffer, AbstractBlockBase<?> block,
  86                     boolean zapRegisters, boolean zapStack) {
  87         DebugContext debug = lir.getDebug();
  88         try (Indent indent = debug.logAndIndent("Process block %s", block)) {
  89             ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
  90             buffer.init(instructions);
  91             for (int index = 0; index < instructions.size(); index++) {
  92                 LIRInstruction inst = instructions.get(index);
  93                 if (zapStack && inst instanceof ZapStackArgumentSpaceBeforeInstruction) {
  94                     LIRInstruction zap = diagnosticLirGenTool.zapArgumentSpace();
  95                     if (zap != null) {
  96                         buffer.append(index, zap);
  97                     }
  98                 }
  99                 if (zapRegisters && inst instanceof ZapRegistersAfterInstruction) {
 100                     LIRFrameState state = getLIRState(inst);
 101                     if (state != null) {
 102                         SaveRegistersOp zap = diagnosticLirGenTool.createZapRegisters();
 103                         SaveRegistersOp old = res.getCalleeSaveInfo().put(state, zap);
 104                         assert old == null : "Already another SaveRegisterOp registered! " + old;














 105                         buffer.append(index + 1, (LIRInstruction) zap);
 106                         debug.log("Insert ZapRegister after %s", inst);
 107                     }
 108                 }
 109             }
 110             buffer.finish();
 111         }
 112     }
 113 
 114     /**
 115      * Returns the {@link LIRFrameState} of an instruction.
 116      */
 117     private static LIRFrameState getLIRState(LIRInstruction inst) {
 118         final LIRFrameState[] lirState = {null};
 119         inst.forEachState(state -> {
 120             assert lirState[0] == null : "Multiple states: " + inst;
 121             lirState[0] = state;
 122         });
 123         assert lirState[0] != null : "No state: " + inst;
 124         return lirState[0];
 125     }
 126 
 127 }
   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 }
< prev index next >