1 /*
   2  * Copyright (c) 2015, 2015, 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.lsra.ssa;
  24 
  25 import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
  26 import static jdk.vm.ci.code.ValueUtil.isRegister;
  27 
  28 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  29 import org.graalvm.compiler.debug.Debug;
  30 import org.graalvm.compiler.debug.Indent;
  31 import org.graalvm.compiler.lir.LIRInstruction;
  32 import org.graalvm.compiler.lir.StandardOp.LabelOp;
  33 import org.graalvm.compiler.lir.StandardOp.MoveOp;
  34 import org.graalvm.compiler.lir.alloc.lsra.Interval;
  35 import org.graalvm.compiler.lir.alloc.lsra.LinearScan;
  36 import org.graalvm.compiler.lir.alloc.lsra.LinearScanEliminateSpillMovePhase;
  37 
  38 public class SSALinearScanEliminateSpillMovePhase extends LinearScanEliminateSpillMovePhase {
  39 
  40     SSALinearScanEliminateSpillMovePhase(LinearScan allocator) {
  41         super(allocator);
  42     }
  43 
  44     @Override
  45     protected int firstInstructionOfInterest() {
  46         // also look at Labels as they define PHI values
  47         return 0;
  48     }
  49 
  50     @Override
  51     protected boolean canEliminateSpillMove(AbstractBlockBase<?> block, MoveOp move) {
  52         if (super.canEliminateSpillMove(block, move)) {
  53             // SSA Linear Scan might introduce moves to stack slots
  54             Interval curInterval = allocator.intervalFor(move.getResult());
  55             assert !isRegister(curInterval.location()) && curInterval.alwaysInMemory();
  56             if (!isPhiResolutionMove(block, move, curInterval)) {
  57                 assert isStackSlotValue(curInterval.location()) : "Not a stack slot: " + curInterval.location();
  58                 return true;
  59             }
  60         }
  61         return false;
  62     }
  63 
  64     @SuppressWarnings("try")
  65     private boolean isPhiResolutionMove(AbstractBlockBase<?> block, MoveOp move, Interval toInterval) {
  66         if (!toInterval.isSplitParent()) {
  67             return false;
  68         }
  69         if ((toInterval.from() & 1) == 1) {
  70             // phi intervals start at even positions.
  71             return false;
  72         }
  73         if (block.getSuccessorCount() != 1) {
  74             return false;
  75         }
  76         LIRInstruction op = allocator.instructionForId(toInterval.from());
  77         if (!(op instanceof LabelOp)) {
  78             return false;
  79         }
  80         AbstractBlockBase<?> intStartBlock = allocator.blockForId(toInterval.from());
  81         assert allocator.getLIR().getLIRforBlock(intStartBlock).get(0).equals(op);
  82         if (!block.getSuccessors()[0].equals(intStartBlock)) {
  83             return false;
  84         }
  85         try (Indent indet = Debug.indent()) {
  86             Debug.log("Is a move (%s) to phi interval %s", move, toInterval);
  87         }
  88         return true;
  89     }
  90 }