1 /* 2 * Copyright (c) 2009, 2016, 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.core.gen; 24 25 import static jdk.vm.ci.code.ValueUtil.asRegister; 26 import static jdk.vm.ci.code.ValueUtil.isLegal; 27 import static jdk.vm.ci.code.ValueUtil.isRegister; 28 import static org.graalvm.compiler.core.common.GraalOptions.MatchExpressions; 29 import static org.graalvm.compiler.debug.DebugOptions.LogVerbose; 30 import static org.graalvm.compiler.lir.LIR.verifyBlock; 31 32 import java.util.ArrayList; 33 import java.util.Collection; 34 import java.util.List; 35 36 import org.graalvm.compiler.core.common.LIRKind; 37 import org.graalvm.compiler.core.common.calc.Condition; 38 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 39 import org.graalvm.compiler.core.common.cfg.BlockMap; 40 import org.graalvm.compiler.core.common.type.Stamp; 41 import org.graalvm.compiler.core.match.ComplexMatchValue; 42 import org.graalvm.compiler.core.match.MatchPattern; 43 import org.graalvm.compiler.core.match.MatchRuleRegistry; 44 import org.graalvm.compiler.core.match.MatchStatement; 45 import org.graalvm.compiler.debug.DebugContext; 46 import org.graalvm.compiler.debug.GraalError; 47 import org.graalvm.compiler.debug.TTY; 48 import org.graalvm.compiler.graph.GraalGraphError; 49 import org.graalvm.compiler.graph.Node; 50 import org.graalvm.compiler.graph.NodeMap; 51 import org.graalvm.compiler.graph.NodeSourcePosition; 52 import org.graalvm.compiler.graph.iterators.NodeIterable; 53 import org.graalvm.compiler.lir.FullInfopointOp; 54 import org.graalvm.compiler.lir.LIRFrameState; 55 import org.graalvm.compiler.lir.LIRInstruction; 56 import org.graalvm.compiler.lir.LabelRef; 57 import org.graalvm.compiler.lir.StandardOp.JumpOp; 58 import org.graalvm.compiler.lir.StandardOp.LabelOp; 59 import org.graalvm.compiler.lir.SwitchStrategy; 60 import org.graalvm.compiler.lir.Variable; 61 import org.graalvm.compiler.lir.debug.LIRGenerationDebugContext; 62 import org.graalvm.compiler.lir.framemap.FrameMapBuilder; 63 import org.graalvm.compiler.lir.gen.LIRGenerator; 64 import org.graalvm.compiler.lir.gen.LIRGenerator.Options; 65 import org.graalvm.compiler.lir.gen.LIRGeneratorTool; 66 import org.graalvm.compiler.lir.gen.LIRGeneratorTool.BlockScope; 67 import org.graalvm.compiler.nodes.AbstractBeginNode; 68 import org.graalvm.compiler.nodes.AbstractEndNode; 69 import org.graalvm.compiler.nodes.AbstractMergeNode; 70 import org.graalvm.compiler.nodes.ConstantNode; 71 import org.graalvm.compiler.nodes.DeoptimizingNode; 72 import org.graalvm.compiler.nodes.DirectCallTargetNode; 73 import org.graalvm.compiler.nodes.FixedNode; 74 import org.graalvm.compiler.nodes.FrameState; 75 import org.graalvm.compiler.nodes.FullInfopointNode; 76 import org.graalvm.compiler.nodes.IfNode; 77 import org.graalvm.compiler.nodes.IndirectCallTargetNode; 78 import org.graalvm.compiler.nodes.Invoke; 79 import org.graalvm.compiler.nodes.InvokeWithExceptionNode; 80 import org.graalvm.compiler.nodes.LogicConstantNode; 81 import org.graalvm.compiler.nodes.LogicNode; 82 import org.graalvm.compiler.nodes.LoopEndNode; 83 import org.graalvm.compiler.nodes.LoweredCallTargetNode; 84 import org.graalvm.compiler.nodes.ParameterNode; 85 import org.graalvm.compiler.nodes.PhiNode; 86 import org.graalvm.compiler.nodes.StructuredGraph; 87 import org.graalvm.compiler.nodes.ValueNode; 88 import org.graalvm.compiler.nodes.ValuePhiNode; 89 import org.graalvm.compiler.nodes.calc.CompareNode; 90 import org.graalvm.compiler.nodes.calc.ConditionalNode; 91 import org.graalvm.compiler.nodes.calc.IntegerTestNode; 92 import org.graalvm.compiler.nodes.calc.IsNullNode; 93 import org.graalvm.compiler.nodes.cfg.Block; 94 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; 95 import org.graalvm.compiler.nodes.extended.IntegerSwitchNode; 96 import org.graalvm.compiler.nodes.extended.SwitchNode; 97 import org.graalvm.compiler.nodes.spi.LIRLowerable; 98 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 99 import org.graalvm.compiler.nodes.spi.NodeValueMap; 100 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 101 import org.graalvm.compiler.options.OptionValues; 102 import org.graalvm.util.EconomicMap; 103 import org.graalvm.util.UnmodifiableMapCursor; 104 105 import jdk.vm.ci.code.CallingConvention; 106 import jdk.vm.ci.code.StackSlot; 107 import jdk.vm.ci.code.ValueUtil; 108 import jdk.vm.ci.meta.AllocatableValue; 109 import jdk.vm.ci.meta.Constant; 110 import jdk.vm.ci.meta.JavaConstant; 111 import jdk.vm.ci.meta.JavaKind; 112 import jdk.vm.ci.meta.PlatformKind; 113 import jdk.vm.ci.meta.Value; 114 115 /** 116 * This class traverses the HIR instructions and generates LIR instructions from them. 117 */ 118 public abstract class NodeLIRBuilder implements NodeLIRBuilderTool, LIRGenerationDebugContext { 119 120 private final NodeMap<Value> nodeOperands; 121 private final DebugInfoBuilder debugInfoBuilder; 122 private final int traceLIRGeneratorLevel; 123 124 protected final LIRGenerator gen; 125 126 private ValueNode currentInstruction; 127 private ValueNode lastInstructionPrinted; // Debugging only 128 129 private final NodeMatchRules nodeMatchRules; 130 private EconomicMap<Class<? extends Node>, List<MatchStatement>> matchRules; 131 132 public NodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool gen, NodeMatchRules nodeMatchRules) { 133 this.gen = (LIRGenerator) gen; 134 this.nodeMatchRules = nodeMatchRules; 135 this.nodeOperands = graph.createNodeMap(); 136 this.debugInfoBuilder = createDebugInfoBuilder(graph, this); 137 OptionValues options = graph.getOptions(); 138 if (MatchExpressions.getValue(options)) { 139 matchRules = MatchRuleRegistry.lookup(nodeMatchRules.getClass(), options, graph.getDebug()); 140 } 141 traceLIRGeneratorLevel = TTY.isSuppressed() ? 0 : Options.TraceLIRGeneratorLevel.getValue(options); 142 143 assert nodeMatchRules.lirBuilder == null; 144 nodeMatchRules.lirBuilder = this; 145 } 146 147 public NodeMatchRules getNodeMatchRules() { 148 return nodeMatchRules; 149 } 150 151 protected DebugInfoBuilder createDebugInfoBuilder(StructuredGraph graph, NodeValueMap nodeValueMap) { 152 return new DebugInfoBuilder(nodeValueMap, graph.getDebug()); 153 } 154 155 /** 156 * Returns the operand that has been previously initialized by 157 * {@link #setResult(ValueNode, Value)} with the result of an instruction. It's a code 158 * generation error to ask for the operand of ValueNode that doesn't have one yet. 159 * 160 * @param node A node that produces a result value. 161 */ 162 @Override 163 public Value operand(Node node) { 164 Value operand = getOperand(node); 165 assert operand != null : String.format("missing operand for %1s", node); 166 return operand; 167 } 168 169 @Override 170 public boolean hasOperand(Node node) { 171 return getOperand(node) != null; 172 } 173 174 private Value getOperand(Node node) { 175 if (nodeOperands == null) { 176 return null; 177 } 178 return nodeOperands.get(node); 179 } 180 181 @Override 182 public ValueNode valueForOperand(Value value) { 183 assert nodeOperands != null; 184 UnmodifiableMapCursor<Node, Value> cursor = nodeOperands.getEntries(); 185 while (cursor.advance()) { 186 if (cursor.getValue().equals(value)) { 187 return (ValueNode) cursor.getKey(); 188 } 189 } 190 return null; 191 } 192 193 @Override 194 public Object getSourceForOperand(Value value) { 195 return valueForOperand(value); 196 } 197 198 @Override 199 public Value setResult(ValueNode x, Value operand) { 200 assert (!isRegister(operand) || !gen.attributes(asRegister(operand)).isAllocatable()); 201 assert nodeOperands != null && (nodeOperands.get(x) == null || nodeOperands.get(x) instanceof ComplexMatchValue) : "operand cannot be set twice"; 202 assert operand != null && isLegal(operand) : "operand must be legal"; 203 assert !(x instanceof VirtualObjectNode); 204 nodeOperands.set(x, operand); 205 return operand; 206 } 207 208 /** 209 * Used by the {@link MatchStatement} machinery to override the generation LIR for some 210 * ValueNodes. 211 */ 212 public void setMatchResult(Node x, Value operand) { 213 assert operand.equals(ComplexMatchValue.INTERIOR_MATCH) || operand instanceof ComplexMatchValue; 214 assert operand instanceof ComplexMatchValue || MatchPattern.isSingleValueUser(x) : "interior matches must be single user"; 215 assert nodeOperands != null && nodeOperands.get(x) == null : "operand cannot be set twice"; 216 assert !(x instanceof VirtualObjectNode); 217 nodeOperands.set(x, operand); 218 } 219 220 public LabelRef getLIRBlock(FixedNode b) { 221 assert gen.getResult().getLIR().getControlFlowGraph() instanceof ControlFlowGraph; 222 Block result = ((ControlFlowGraph) gen.getResult().getLIR().getControlFlowGraph()).blockFor(b); 223 int suxIndex = 0; 224 for (AbstractBlockBase<?> succ : gen.getCurrentBlock().getSuccessors()) { 225 if (succ == result) { 226 assert gen.getCurrentBlock() instanceof Block; 227 return LabelRef.forSuccessor(gen.getResult().getLIR(), gen.getCurrentBlock(), suxIndex); 228 } 229 suxIndex++; 230 } 231 throw GraalError.shouldNotReachHere("Block not in successor list of current block"); 232 } 233 234 public final void append(LIRInstruction op) { 235 if (Options.PrintIRWithLIR.getValue(nodeOperands.graph().getOptions()) && !TTY.isSuppressed()) { 236 if (currentInstruction != null && lastInstructionPrinted != currentInstruction) { 237 lastInstructionPrinted = currentInstruction; 238 InstructionPrinter ip = new InstructionPrinter(TTY.out()); 239 ip.printInstructionListing(currentInstruction); 240 } 241 } 242 gen.append(op); 243 } 244 245 protected LIRKind getExactPhiKind(PhiNode phi) { 246 LIRKind derivedKind = gen.toRegisterKind(gen.getLIRKind(phi.stamp())); 247 /* Collect reference information. */ 248 for (int i = 0; i < phi.valueCount() && !derivedKind.isUnknownReference(); i++) { 249 ValueNode node = phi.valueAt(i); 250 Value value = getOperand(node); 251 252 // get ValueKind for input 253 final LIRKind valueKind; 254 if (value != null) { 255 valueKind = value.getValueKind(LIRKind.class); 256 } else { 257 assert isPhiInputFromBackedge(phi, i) : String.format("Input %s to phi node %s is not yet available although it is not coming from a loop back edge", node, phi); 258 LIRKind kind = gen.getLIRKind(node.stamp()); 259 valueKind = gen.toRegisterKind(kind); 260 } 261 /* Merge the reference information of the derived kind and the input. */ 262 derivedKind = LIRKind.mergeReferenceInformation(derivedKind, valueKind); 263 } 264 return derivedKind; 265 } 266 267 private static boolean isPhiInputFromBackedge(PhiNode phi, int index) { 268 AbstractMergeNode merge = phi.merge(); 269 AbstractEndNode end = merge.phiPredecessorAt(index); 270 return end instanceof LoopEndNode && ((LoopEndNode) end).loopBegin().equals(merge); 271 } 272 273 private Value[] createPhiIn(AbstractMergeNode merge) { 274 List<Value> values = new ArrayList<>(); 275 for (ValuePhiNode phi : merge.valuePhis()) { 276 assert getOperand(phi) == null; 277 Variable value = gen.newVariable(getExactPhiKind(phi)); 278 values.add(value); 279 setResult(phi, value); 280 } 281 return values.toArray(new Value[values.size()]); 282 } 283 284 /** 285 * @return {@code true} if object constant to stack moves are supported. 286 */ 287 protected boolean allowObjectConstantToStackMove() { 288 return true; 289 } 290 291 private Value[] createPhiOut(AbstractMergeNode merge, AbstractEndNode pred) { 292 List<Value> values = new ArrayList<>(); 293 for (PhiNode phi : merge.valuePhis()) { 294 ValueNode node = phi.valueAt(pred); 295 Value value = operand(node); 296 assert value != null; 297 if (isRegister(value)) { 298 /* 299 * Fixed register intervals are not allowed at block boundaries so we introduce a 300 * new Variable. 301 */ 302 value = gen.emitMove(value); 303 } else if (!allowObjectConstantToStackMove() && node instanceof ConstantNode && !LIRKind.isValue(value)) { 304 /* 305 * Some constants are not allowed as inputs for PHIs in certain backends. Explicitly 306 * create a copy of this value to force it into a register. The new variable is only 307 * used in the PHI. 308 */ 309 Variable result = gen.newVariable(value.getValueKind()); 310 gen.emitMove(result, value); 311 value = result; 312 } 313 values.add(value); 314 } 315 return values.toArray(new Value[values.size()]); 316 } 317 318 @Override 319 @SuppressWarnings("try") 320 public void doBlock(Block block, StructuredGraph graph, BlockMap<List<Node>> blockMap) { 321 322 OptionValues options = graph.getOptions(); 323 try (BlockScope blockScope = gen.getBlockScope(block)) { 324 setSourcePosition(null); 325 326 if (block == gen.getResult().getLIR().getControlFlowGraph().getStartBlock()) { 327 assert block.getPredecessorCount() == 0; 328 emitPrologue(graph); 329 } else { 330 assert block.getPredecessorCount() > 0; 331 // create phi-in value array 332 AbstractBeginNode begin = block.getBeginNode(); 333 if (begin instanceof AbstractMergeNode) { 334 AbstractMergeNode merge = (AbstractMergeNode) begin; 335 LabelOp label = (LabelOp) gen.getResult().getLIR().getLIRforBlock(block).get(0); 336 label.setPhiValues(createPhiIn(merge)); 337 if (Options.PrintIRWithLIR.getValue(options) && !TTY.isSuppressed()) { 338 TTY.println("Created PhiIn: " + label); 339 340 } 341 } 342 } 343 344 List<Node> nodes = blockMap.get(block); 345 346 // Allow NodeLIRBuilder subclass to specialize code generation of any interesting groups 347 // of instructions 348 matchComplexExpressions(nodes); 349 350 boolean trace = traceLIRGeneratorLevel >= 3; 351 for (int i = 0; i < nodes.size(); i++) { 352 Node node = nodes.get(i); 353 if (node instanceof ValueNode) { 354 DebugContext debug = node.getDebug(); 355 ValueNode valueNode = (ValueNode) node; 356 if (trace) { 357 TTY.println("LIRGen for " + valueNode); 358 } 359 Value operand = getOperand(valueNode); 360 if (operand == null) { 361 if (!peephole(valueNode)) { 362 try { 363 doRoot(valueNode); 364 } catch (GraalError e) { 365 throw GraalGraphError.transformAndAddContext(e, valueNode); 366 } catch (Throwable e) { 367 throw new GraalGraphError(e).addContext(valueNode); 368 } 369 } 370 } else if (ComplexMatchValue.INTERIOR_MATCH.equals(operand)) { 371 // Doesn't need to be evaluated 372 debug.log("interior match for %s", valueNode); 373 } else if (operand instanceof ComplexMatchValue) { 374 debug.log("complex match for %s", valueNode); 375 ComplexMatchValue match = (ComplexMatchValue) operand; 376 operand = match.evaluate(this); 377 if (operand != null) { 378 setResult(valueNode, operand); 379 } 380 } else { 381 // There can be cases in which the result of an instruction is already set 382 // before by other instructions. 383 } 384 } 385 } 386 387 if (!gen.hasBlockEnd(block)) { 388 NodeIterable<Node> successors = block.getEndNode().successors(); 389 assert successors.count() == block.getSuccessorCount(); 390 if (block.getSuccessorCount() != 1) { 391 /* 392 * If we have more than one successor, we cannot just use the first one. Since 393 * successors are unordered, this would be a random choice. 394 */ 395 throw new GraalError("Block without BlockEndOp: " + block.getEndNode()); 396 } 397 gen.emitJump(getLIRBlock((FixedNode) successors.first())); 398 } 399 400 assert verifyBlock(gen.getResult().getLIR(), block); 401 } 402 } 403 404 @SuppressWarnings("try") 405 protected void matchComplexExpressions(List<Node> nodes) { 406 if (matchRules != null) { 407 DebugContext debug = gen.getResult().getLIR().getDebug(); 408 try (DebugContext.Scope s = debug.scope("MatchComplexExpressions")) { 409 if (LogVerbose.getValue(nodeOperands.graph().getOptions())) { 410 int i = 0; 411 for (Node node : nodes) { 412 debug.log("%d: (%s) %1S", i++, node.getUsageCount(), node); 413 } 414 } 415 416 // Match the nodes in backwards order to encourage longer matches. 417 for (int index = nodes.size() - 1; index >= 0; index--) { 418 Node node = nodes.get(index); 419 if (getOperand(node) != null) { 420 continue; 421 } 422 // See if this node is the root of any MatchStatements 423 List<MatchStatement> statements = matchRules.get(node.getClass()); 424 if (statements != null) { 425 for (MatchStatement statement : statements) { 426 if (statement.generate(this, index, node, nodes)) { 427 // Found a match so skip to the next 428 break; 429 } 430 } 431 } 432 } 433 } 434 } 435 } 436 437 protected abstract boolean peephole(ValueNode valueNode); 438 439 private void doRoot(ValueNode instr) { 440 if (traceLIRGeneratorLevel >= 2) { 441 TTY.println("Emitting LIR for instruction " + instr); 442 } 443 currentInstruction = instr; 444 DebugContext debug = instr.getDebug(); 445 debug.log("Visiting %s", instr); 446 emitNode(instr); 447 debug.log("Operand for %s = %s", instr, getOperand(instr)); 448 } 449 450 protected void emitNode(ValueNode node) { 451 if (node.getDebug().isLogEnabled() && node.stamp().isEmpty()) { 452 node.getDebug().log("This node has an empty stamp, we are emitting dead code(?): %s", node); 453 } 454 setSourcePosition(node.getNodeSourcePosition()); 455 if (node instanceof LIRLowerable) { 456 ((LIRLowerable) node).generate(this); 457 } else { 458 throw GraalError.shouldNotReachHere("node is not LIRLowerable: " + node); 459 } 460 } 461 462 protected void emitPrologue(StructuredGraph graph) { 463 CallingConvention incomingArguments = gen.getResult().getCallingConvention(); 464 465 Value[] params = new Value[incomingArguments.getArgumentCount()]; 466 for (int i = 0; i < params.length; i++) { 467 params[i] = incomingArguments.getArgument(i); 468 if (ValueUtil.isStackSlot(params[i])) { 469 StackSlot slot = ValueUtil.asStackSlot(params[i]); 470 if (slot.isInCallerFrame() && !gen.getResult().getLIR().hasArgInCallerFrame()) { 471 gen.getResult().getLIR().setHasArgInCallerFrame(); 472 } 473 } 474 } 475 476 gen.emitIncomingValues(params); 477 478 for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) { 479 Value paramValue = params[param.index()]; 480 assert paramValue.getValueKind().equals(getLIRGeneratorTool().getLIRKind(param.stamp())) : paramValue + " " + getLIRGeneratorTool().getLIRKind(param.stamp()); 481 setResult(param, gen.emitMove(paramValue)); 482 } 483 } 484 485 @Override 486 public void visitMerge(AbstractMergeNode x) { 487 } 488 489 @Override 490 public void visitEndNode(AbstractEndNode end) { 491 AbstractMergeNode merge = end.merge(); 492 JumpOp jump = newJumpOp(getLIRBlock(merge)); 493 jump.setPhiValues(createPhiOut(merge, end)); 494 append(jump); 495 } 496 497 /** 498 * Runtime specific classes can override this to insert a safepoint at the end of a loop. 499 */ 500 @Override 501 public void visitLoopEnd(LoopEndNode x) { 502 } 503 504 protected JumpOp newJumpOp(LabelRef ref) { 505 return new JumpOp(ref); 506 } 507 508 protected LIRKind getPhiKind(PhiNode phi) { 509 return gen.getLIRKind(phi.stamp()); 510 } 511 512 @Override 513 public void emitIf(IfNode x) { 514 emitBranch(x.condition(), getLIRBlock(x.trueSuccessor()), getLIRBlock(x.falseSuccessor()), x.probability(x.trueSuccessor())); 515 } 516 517 public void emitBranch(LogicNode node, LabelRef trueSuccessor, LabelRef falseSuccessor, double trueSuccessorProbability) { 518 if (node instanceof IsNullNode) { 519 emitNullCheckBranch((IsNullNode) node, trueSuccessor, falseSuccessor, trueSuccessorProbability); 520 } else if (node instanceof CompareNode) { 521 emitCompareBranch((CompareNode) node, trueSuccessor, falseSuccessor, trueSuccessorProbability); 522 } else if (node instanceof LogicConstantNode) { 523 emitConstantBranch(((LogicConstantNode) node).getValue(), trueSuccessor, falseSuccessor); 524 } else if (node instanceof IntegerTestNode) { 525 emitIntegerTestBranch((IntegerTestNode) node, trueSuccessor, falseSuccessor, trueSuccessorProbability); 526 } else { 527 throw GraalError.unimplemented(node.toString()); 528 } 529 } 530 531 private void emitNullCheckBranch(IsNullNode node, LabelRef trueSuccessor, LabelRef falseSuccessor, double trueSuccessorProbability) { 532 LIRKind kind = gen.getLIRKind(node.getValue().stamp()); 533 Value nullValue = gen.emitConstant(kind, JavaConstant.NULL_POINTER); 534 gen.emitCompareBranch(kind.getPlatformKind(), operand(node.getValue()), nullValue, Condition.EQ, false, trueSuccessor, falseSuccessor, trueSuccessorProbability); 535 } 536 537 public void emitCompareBranch(CompareNode compare, LabelRef trueSuccessor, LabelRef falseSuccessor, double trueSuccessorProbability) { 538 PlatformKind kind = gen.getLIRKind(compare.getX().stamp()).getPlatformKind(); 539 gen.emitCompareBranch(kind, operand(compare.getX()), operand(compare.getY()), compare.condition(), compare.unorderedIsTrue(), trueSuccessor, falseSuccessor, trueSuccessorProbability); 540 } 541 542 public void emitIntegerTestBranch(IntegerTestNode test, LabelRef trueSuccessor, LabelRef falseSuccessor, double trueSuccessorProbability) { 543 gen.emitIntegerTestBranch(operand(test.getX()), operand(test.getY()), trueSuccessor, falseSuccessor, trueSuccessorProbability); 544 } 545 546 public void emitConstantBranch(boolean value, LabelRef trueSuccessorBlock, LabelRef falseSuccessorBlock) { 547 LabelRef block = value ? trueSuccessorBlock : falseSuccessorBlock; 548 gen.emitJump(block); 549 } 550 551 @Override 552 public void emitConditional(ConditionalNode conditional) { 553 Value tVal = operand(conditional.trueValue()); 554 Value fVal = operand(conditional.falseValue()); 555 setResult(conditional, emitConditional(conditional.condition(), tVal, fVal)); 556 } 557 558 public Variable emitConditional(LogicNode node, Value trueValue, Value falseValue) { 559 if (node instanceof IsNullNode) { 560 IsNullNode isNullNode = (IsNullNode) node; 561 LIRKind kind = gen.getLIRKind(isNullNode.getValue().stamp()); 562 Value nullValue = gen.emitConstant(kind, JavaConstant.NULL_POINTER); 563 return gen.emitConditionalMove(kind.getPlatformKind(), operand(isNullNode.getValue()), nullValue, Condition.EQ, false, trueValue, falseValue); 564 } else if (node instanceof CompareNode) { 565 CompareNode compare = (CompareNode) node; 566 PlatformKind kind = gen.getLIRKind(compare.getX().stamp()).getPlatformKind(); 567 return gen.emitConditionalMove(kind, operand(compare.getX()), operand(compare.getY()), compare.condition(), compare.unorderedIsTrue(), trueValue, falseValue); 568 } else if (node instanceof LogicConstantNode) { 569 return gen.emitMove(((LogicConstantNode) node).getValue() ? trueValue : falseValue); 570 } else if (node instanceof IntegerTestNode) { 571 IntegerTestNode test = (IntegerTestNode) node; 572 return gen.emitIntegerTestMove(operand(test.getX()), operand(test.getY()), trueValue, falseValue); 573 } else { 574 throw GraalError.unimplemented(node.toString()); 575 } 576 } 577 578 @Override 579 public void emitInvoke(Invoke x) { 580 LoweredCallTargetNode callTarget = (LoweredCallTargetNode) x.callTarget(); 581 FrameMapBuilder frameMapBuilder = gen.getResult().getFrameMapBuilder(); 582 CallingConvention invokeCc = frameMapBuilder.getRegisterConfig().getCallingConvention(callTarget.callType(), x.asNode().stamp().javaType(gen.getMetaAccess()), callTarget.signature(), gen); 583 frameMapBuilder.callsMethod(invokeCc); 584 585 Value[] parameters = visitInvokeArguments(invokeCc, callTarget.arguments()); 586 587 LabelRef exceptionEdge = null; 588 if (x instanceof InvokeWithExceptionNode) { 589 exceptionEdge = getLIRBlock(((InvokeWithExceptionNode) x).exceptionEdge()); 590 } 591 LIRFrameState callState = stateWithExceptionEdge(x, exceptionEdge); 592 593 Value result = invokeCc.getReturn(); 594 if (callTarget instanceof DirectCallTargetNode) { 595 emitDirectCall((DirectCallTargetNode) callTarget, result, parameters, AllocatableValue.NONE, callState); 596 } else if (callTarget instanceof IndirectCallTargetNode) { 597 emitIndirectCall((IndirectCallTargetNode) callTarget, result, parameters, AllocatableValue.NONE, callState); 598 } else { 599 throw GraalError.shouldNotReachHere(); 600 } 601 602 if (isLegal(result)) { 603 setResult(x.asNode(), gen.emitMove(result)); 604 } 605 606 if (x instanceof InvokeWithExceptionNode) { 607 gen.emitJump(getLIRBlock(((InvokeWithExceptionNode) x).next())); 608 } 609 } 610 611 protected abstract void emitDirectCall(DirectCallTargetNode callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState callState); 612 613 protected abstract void emitIndirectCall(IndirectCallTargetNode callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState callState); 614 615 @Override 616 public Value[] visitInvokeArguments(CallingConvention invokeCc, Collection<ValueNode> arguments) { 617 // for each argument, load it into the correct location 618 Value[] result = new Value[arguments.size()]; 619 int j = 0; 620 for (ValueNode arg : arguments) { 621 if (arg != null) { 622 AllocatableValue operand = invokeCc.getArgument(j); 623 gen.emitMove(operand, operand(arg)); 624 result[j] = operand; 625 j++; 626 } else { 627 throw GraalError.shouldNotReachHere("I thought we no longer have null entries for two-slot types..."); 628 } 629 } 630 return result; 631 } 632 633 /** 634 * This method tries to create a switch implementation that is optimal for the given switch. It 635 * will either generate a sequential if/then/else cascade, a set of range tests or a table 636 * switch. 637 * 638 * If the given switch does not contain int keys, it will always create a sequential 639 * implementation. 640 */ 641 @Override 642 public void emitSwitch(SwitchNode x) { 643 assert x.defaultSuccessor() != null; 644 LabelRef defaultTarget = getLIRBlock(x.defaultSuccessor()); 645 int keyCount = x.keyCount(); 646 if (keyCount == 0) { 647 gen.emitJump(defaultTarget); 648 } else { 649 Variable value = gen.load(operand(x.value())); 650 if (keyCount == 1) { 651 assert defaultTarget != null; 652 double probability = x.probability(x.keySuccessor(0)); 653 LIRKind kind = gen.getLIRKind(x.value().stamp()); 654 Value key = gen.emitConstant(kind, x.keyAt(0)); 655 gen.emitCompareBranch(kind.getPlatformKind(), gen.load(operand(x.value())), key, Condition.EQ, false, getLIRBlock(x.keySuccessor(0)), defaultTarget, probability); 656 } else if (x instanceof IntegerSwitchNode && x.isSorted()) { 657 IntegerSwitchNode intSwitch = (IntegerSwitchNode) x; 658 LabelRef[] keyTargets = new LabelRef[keyCount]; 659 JavaConstant[] keyConstants = new JavaConstant[keyCount]; 660 double[] keyProbabilities = new double[keyCount]; 661 JavaKind keyKind = intSwitch.keyAt(0).getJavaKind(); 662 for (int i = 0; i < keyCount; i++) { 663 keyTargets[i] = getLIRBlock(intSwitch.keySuccessor(i)); 664 keyConstants[i] = intSwitch.keyAt(i); 665 keyProbabilities[i] = intSwitch.keyProbability(i); 666 assert keyConstants[i].getJavaKind() == keyKind; 667 } 668 gen.emitStrategySwitch(keyConstants, keyProbabilities, keyTargets, defaultTarget, value); 669 } else { 670 // keyKind != JavaKind.Int || !x.isSorted() 671 LabelRef[] keyTargets = new LabelRef[keyCount]; 672 Constant[] keyConstants = new Constant[keyCount]; 673 double[] keyProbabilities = new double[keyCount]; 674 for (int i = 0; i < keyCount; i++) { 675 keyTargets[i] = getLIRBlock(x.keySuccessor(i)); 676 keyConstants[i] = x.keyAt(i); 677 keyProbabilities[i] = x.keyProbability(i); 678 } 679 680 // hopefully only a few entries 681 gen.emitStrategySwitch(new SwitchStrategy.SequentialStrategy(keyProbabilities, keyConstants), value, keyTargets, defaultTarget); 682 } 683 } 684 } 685 686 public DebugInfoBuilder getDebugInfoBuilder() { 687 assert debugInfoBuilder != null; 688 return debugInfoBuilder; 689 } 690 691 private static FrameState getFrameState(DeoptimizingNode deopt) { 692 if (deopt instanceof DeoptimizingNode.DeoptBefore) { 693 assert !(deopt instanceof DeoptimizingNode.DeoptDuring || deopt instanceof DeoptimizingNode.DeoptAfter); 694 return ((DeoptimizingNode.DeoptBefore) deopt).stateBefore(); 695 } else if (deopt instanceof DeoptimizingNode.DeoptDuring) { 696 assert !(deopt instanceof DeoptimizingNode.DeoptAfter); 697 return ((DeoptimizingNode.DeoptDuring) deopt).stateDuring(); 698 } else { 699 assert deopt instanceof DeoptimizingNode.DeoptAfter; 700 return ((DeoptimizingNode.DeoptAfter) deopt).stateAfter(); 701 } 702 } 703 704 @Override 705 public LIRFrameState state(DeoptimizingNode deopt) { 706 if (!deopt.canDeoptimize()) { 707 return null; 708 } 709 return stateFor(getFrameState(deopt)); 710 } 711 712 public LIRFrameState stateWithExceptionEdge(DeoptimizingNode deopt, LabelRef exceptionEdge) { 713 if (!deopt.canDeoptimize()) { 714 return null; 715 } 716 return stateForWithExceptionEdge(getFrameState(deopt), exceptionEdge); 717 } 718 719 public LIRFrameState stateFor(FrameState state) { 720 return stateForWithExceptionEdge(state, null); 721 } 722 723 public LIRFrameState stateForWithExceptionEdge(FrameState state, LabelRef exceptionEdge) { 724 if (gen.needOnlyOopMaps()) { 725 return new LIRFrameState(null, null, null); 726 } 727 assert state != null; 728 return getDebugInfoBuilder().build(state, exceptionEdge); 729 } 730 731 @Override 732 public void emitOverflowCheckBranch(AbstractBeginNode overflowSuccessor, AbstractBeginNode next, Stamp stamp, double probability) { 733 LIRKind cmpKind = getLIRGeneratorTool().getLIRKind(stamp); 734 gen.emitOverflowCheckBranch(getLIRBlock(overflowSuccessor), getLIRBlock(next), cmpKind, probability); 735 } 736 737 @Override 738 public void visitFullInfopointNode(FullInfopointNode i) { 739 append(new FullInfopointOp(stateFor(i.getState()), i.getReason())); 740 } 741 742 @Override 743 public void setSourcePosition(NodeSourcePosition position) { 744 gen.setSourcePosition(position); 745 } 746 747 @Override 748 public LIRGeneratorTool getLIRGeneratorTool() { 749 return gen; 750 } 751 }