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