1 /*
   2  * Copyright (c) 2009, 2012, 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;
  24 
  25 import java.util.ArrayList;
  26 import java.util.Arrays;
  27 import java.util.List;
  28 
  29 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  30 import org.graalvm.compiler.core.common.cfg.AbstractControlFlowGraph;
  31 import org.graalvm.compiler.core.common.cfg.BlockMap;
  32 import org.graalvm.compiler.lir.StandardOp.BlockEndOp;
  33 import org.graalvm.compiler.lir.StandardOp.LabelOp;
  34 import org.graalvm.compiler.lir.gen.LIRGenerator;
  35 
  36 /**
  37  * This class implements the overall container for the LIR graph and directs its construction,
  38  * optimization, and finalization.
  39  */
  40 public final class LIR extends LIRGenerator.VariableProvider {
  41 
  42     private final AbstractControlFlowGraph<?> cfg;
  43 
  44     /**
  45      * The linear-scan ordered list of blocks.
  46      */
  47     private final AbstractBlockBase<?>[] linearScanOrder;
  48 
  49     /**
  50      * The order in which the code is emitted.
  51      */
  52     private final AbstractBlockBase<?>[] codeEmittingOrder;
  53 
  54     /**
  55      * Map from {@linkplain AbstractBlockBase block} to {@linkplain LIRInstruction}s. Note that we
  56      * are using {@link ArrayList} instead of {@link List} to avoid interface dispatch.
  57      */
  58     private final BlockMap<ArrayList<LIRInstruction>> lirInstructions;
  59 
  60     private boolean hasArgInCallerFrame;
  61 
  62     /**
  63      * Creates a new LIR instance for the specified compilation.
  64      */
  65     public LIR(AbstractControlFlowGraph<?> cfg, AbstractBlockBase<?>[] linearScanOrder, AbstractBlockBase<?>[] codeEmittingOrder) {
  66         this.cfg = cfg;
  67         this.codeEmittingOrder = codeEmittingOrder;
  68         this.linearScanOrder = linearScanOrder;
  69         this.lirInstructions = new BlockMap<>(cfg);
  70     }
  71 
  72     public AbstractControlFlowGraph<?> getControlFlowGraph() {
  73         return cfg;
  74     }
  75 
  76     /**
  77      * Determines if any instruction in the LIR has debug info associated with it.
  78      */
  79     public boolean hasDebugInfo() {
  80         for (AbstractBlockBase<?> b : linearScanOrder()) {
  81             for (LIRInstruction op : getLIRforBlock(b)) {
  82                 if (op.hasState()) {
  83                     return true;
  84                 }
  85             }
  86         }
  87         return false;
  88     }
  89 
  90     public ArrayList<LIRInstruction> getLIRforBlock(AbstractBlockBase<?> block) {
  91         return lirInstructions.get(block);
  92     }
  93 
  94     public void setLIRforBlock(AbstractBlockBase<?> block, ArrayList<LIRInstruction> list) {
  95         assert getLIRforBlock(block) == null : "lir instruction list should only be initialized once";
  96         lirInstructions.put(block, list);
  97     }
  98 
  99     /**
 100      * Gets the linear scan ordering of blocks as an array.
 101      *
 102      * @return the blocks in linear scan order
 103      */
 104     public AbstractBlockBase<?>[] linearScanOrder() {
 105         return linearScanOrder;
 106     }
 107 
 108     public AbstractBlockBase<?>[] codeEmittingOrder() {
 109         return codeEmittingOrder;
 110     }
 111 
 112     public void setHasArgInCallerFrame() {
 113         hasArgInCallerFrame = true;
 114     }
 115 
 116     /**
 117      * Determines if any of the parameters to the method are passed via the stack where the
 118      * parameters are located in the caller's frame.
 119      */
 120     public boolean hasArgInCallerFrame() {
 121         return hasArgInCallerFrame;
 122     }
 123 
 124     /**
 125      * Gets the next non-{@code null} block in a list.
 126      *
 127      * @param blocks list of blocks
 128      * @param blockIndex index of the current block
 129      * @return the next block in the list that is none {@code null} or {@code null} if there is no
 130      *         such block
 131      */
 132     public static AbstractBlockBase<?> getNextBlock(AbstractBlockBase<?>[] blocks, int blockIndex) {
 133         for (int nextIndex = blockIndex + 1; nextIndex > 0 && nextIndex < blocks.length; nextIndex++) {
 134             AbstractBlockBase<?> nextBlock = blocks[nextIndex];
 135             if (nextBlock != null) {
 136                 return nextBlock;
 137             }
 138         }
 139         return null;
 140     }
 141 
 142     /**
 143      * Gets the exception edge (if any) originating at a given operation.
 144      */
 145     public static LabelRef getExceptionEdge(LIRInstruction op) {
 146         final LabelRef[] exceptionEdge = {null};
 147         op.forEachState(state -> {
 148             if (state.exceptionEdge != null) {
 149                 assert exceptionEdge[0] == null;
 150                 exceptionEdge[0] = state.exceptionEdge;
 151             }
 152         });
 153         return exceptionEdge[0];
 154     }
 155 
 156     /**
 157      * The maximum distance an operation with an {@linkplain #getExceptionEdge(LIRInstruction)
 158      * exception edge} can be from the last instruction of a LIR block. The value of 3 is based on a
 159      * non-void call operation that has an exception edge. Such a call may move the result to
 160      * another register and then spill it.
 161      * <p>
 162      * The rationale for such a constant is to limit the search for an insertion point when adding
 163      * move operations at the end of a block. Such moves must be inserted before all control flow
 164      * instructions.
 165      */
 166     public static final int MAX_EXCEPTION_EDGE_OP_DISTANCE_FROM_END = 3;
 167 
 168     public static boolean verifyBlock(LIR lir, AbstractBlockBase<?> block) {
 169         ArrayList<LIRInstruction> ops = lir.getLIRforBlock(block);
 170         if (ops.size() == 0) {
 171             return false;
 172         }
 173         assert ops.get(0) instanceof LabelOp : String.format("Not a Label %s (Block %s)", ops.get(0).getClass(), block);
 174         LIRInstruction opWithExceptionEdge = null;
 175         int index = 0;
 176         int lastIndex = ops.size() - 1;
 177         for (LIRInstruction op : ops.subList(0, lastIndex)) {
 178             assert !(op instanceof BlockEndOp) : String.format("BlockEndOp %s (Block %s)", op.getClass(), block);
 179             LabelRef exceptionEdge = getExceptionEdge(op);
 180             if (exceptionEdge != null) {
 181                 assert opWithExceptionEdge == null : "multiple ops with an exception edge not allowed";
 182                 opWithExceptionEdge = op;
 183                 int distanceFromEnd = lastIndex - index;
 184                 assert distanceFromEnd <= MAX_EXCEPTION_EDGE_OP_DISTANCE_FROM_END;
 185             }
 186             index++;
 187         }
 188         LIRInstruction end = ops.get(lastIndex);
 189         assert end instanceof BlockEndOp : String.format("Not a BlockEndOp %s (Block %s)", end.getClass(), block);
 190         return true;
 191     }
 192 
 193     public static boolean verifyBlocks(LIR lir, AbstractBlockBase<?>[] blocks) {
 194         for (AbstractBlockBase<?> block : blocks) {
 195             if (block == null) {
 196                 continue;
 197             }
 198             for (AbstractBlockBase<?> sux : block.getSuccessors()) {
 199                 assert Arrays.asList(blocks).contains(sux) : "missing successor from: " + block + "to: " + sux;
 200             }
 201             for (AbstractBlockBase<?> pred : block.getPredecessors()) {
 202                 assert Arrays.asList(blocks).contains(pred) : "missing predecessor from: " + block + "to: " + pred;
 203             }
 204             if (!verifyBlock(lir, block)) {
 205                 return false;
 206             }
 207         }
 208         return true;
 209     }
 210 
 211     public void resetLabels() {
 212 
 213         for (AbstractBlockBase<?> block : codeEmittingOrder()) {
 214             if (block == null) {
 215                 continue;
 216             }
 217             for (LIRInstruction inst : lirInstructions.get(block)) {
 218                 if (inst instanceof LabelOp) {
 219                     ((LabelOp) inst).getLabel().reset();
 220                 }
 221             }
 222         }
 223     }
 224 
 225 }