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