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.GraalDebugConfig.Options.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 import org.graalvm.compiler.core.common.LIRKind; 36 import org.graalvm.compiler.core.common.calc.Condition; 37 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 38 import org.graalvm.compiler.core.common.cfg.BlockMap; 39 import org.graalvm.compiler.core.common.type.Stamp; 40 import org.graalvm.compiler.core.match.ComplexMatchValue; 41 import org.graalvm.compiler.core.match.MatchPattern; 42 import org.graalvm.compiler.core.match.MatchRuleRegistry; 43 import org.graalvm.compiler.core.match.MatchStatement; 44 import org.graalvm.compiler.debug.Debug; 45 import org.graalvm.compiler.debug.Debug.Scope; 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.gen.LIRGenerator; 63 import org.graalvm.compiler.lir.gen.LIRGenerator.Options; 64 import org.graalvm.compiler.lir.gen.LIRGeneratorTool; 65 import org.graalvm.compiler.lir.gen.LIRGeneratorTool.BlockScope; 66 import org.graalvm.compiler.nodes.AbstractBeginNode; 67 import org.graalvm.compiler.nodes.AbstractEndNode; 68 import org.graalvm.compiler.nodes.AbstractMergeNode; 69 import org.graalvm.compiler.nodes.ConstantNode; 70 import org.graalvm.compiler.nodes.DeoptimizingNode; 71 import org.graalvm.compiler.nodes.DirectCallTargetNode; 72 import org.graalvm.compiler.nodes.FixedNode; 73 import org.graalvm.compiler.nodes.FrameState; 74 import org.graalvm.compiler.nodes.FullInfopointNode; 75 import org.graalvm.compiler.nodes.IfNode; 76 import org.graalvm.compiler.nodes.IndirectCallTargetNode; 77 import org.graalvm.compiler.nodes.Invoke; 78 import org.graalvm.compiler.nodes.InvokeWithExceptionNode; 79 import org.graalvm.compiler.nodes.LogicConstantNode; 80 import org.graalvm.compiler.nodes.LogicNode; 81 import org.graalvm.compiler.nodes.LoopEndNode; 82 import org.graalvm.compiler.nodes.LoweredCallTargetNode; 83 import org.graalvm.compiler.nodes.ParameterNode; 84 import org.graalvm.compiler.nodes.PhiNode; 85 import org.graalvm.compiler.nodes.StructuredGraph; 86 import org.graalvm.compiler.nodes.ValueNode; 87 import org.graalvm.compiler.nodes.ValuePhiNode; 88 import org.graalvm.compiler.nodes.calc.CompareNode; 89 import org.graalvm.compiler.nodes.calc.ConditionalNode; 90 import org.graalvm.compiler.nodes.calc.IntegerTestNode; 91 import org.graalvm.compiler.nodes.calc.IsNullNode; 92 import org.graalvm.compiler.nodes.cfg.Block; 93 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; 94 import org.graalvm.compiler.nodes.extended.IntegerSwitchNode; 95 import org.graalvm.compiler.nodes.extended.SwitchNode; 96 import org.graalvm.compiler.nodes.spi.LIRLowerable; 97 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 98 import org.graalvm.compiler.nodes.spi.NodeValueMap; 99 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 100 import org.graalvm.compiler.options.OptionValues; 101 import org.graalvm.util.EconomicMap; 102 import org.graalvm.util.UnmodifiableMapCursor; 103 104 import jdk.vm.ci.code.CallingConvention; 105 import jdk.vm.ci.code.StackSlot; 106 import jdk.vm.ci.code.ValueUtil; 107 import jdk.vm.ci.meta.AllocatableValue; 108 import jdk.vm.ci.meta.Constant; 109 import jdk.vm.ci.meta.JavaConstant; 110 import jdk.vm.ci.meta.JavaKind; 111 import jdk.vm.ci.meta.PlatformKind; 112 import jdk.vm.ci.meta.Value; 113 114 /** 115 * This class traverses the HIR instructions and generates LIR instructions from them. 116 */ 117 public abstract class NodeLIRBuilder implements NodeLIRBuilderTool, LIRGenerationDebugContext { 118 119 private final NodeMap<Value> nodeOperands; 120 private final DebugInfoBuilder debugInfoBuilder; 121 private final int traceLIRGeneratorLevel; 122 123 protected final LIRGenerator gen; 124 125 private ValueNode currentInstruction; 126 private ValueNode lastInstructionPrinted; // Debugging only 127 128 private final NodeMatchRules nodeMatchRules; 129 private EconomicMap<Class<? extends Node>, List<MatchStatement>> matchRules; 130 131 public NodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool gen, NodeMatchRules nodeMatchRules) { 132 this.gen = (LIRGenerator) gen; 133 this.nodeMatchRules = nodeMatchRules; 134 this.nodeOperands = graph.createNodeMap(); 135 this.debugInfoBuilder = createDebugInfoBuilder(graph, this); 136 OptionValues options = graph.getOptions(); 137 if (MatchExpressions.getValue(options)) { 138 matchRules = MatchRuleRegistry.lookup(nodeMatchRules.getClass(), options); 139 } 140 traceLIRGeneratorLevel = TTY.isSuppressed() ? 0 : Options.TraceLIRGeneratorLevel.getValue(options); 141 142 assert nodeMatchRules.lirBuilder == null; 143 nodeMatchRules.lirBuilder = this; 144 } 145 146 public NodeMatchRules getNodeMatchRules() { 147 return nodeMatchRules; 148 } 149 150 @SuppressWarnings({"unused"}) 151 protected DebugInfoBuilder createDebugInfoBuilder(StructuredGraph graph, NodeValueMap nodeValueMap) { 152 return new DebugInfoBuilder(nodeValueMap); 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 ValueNode valueNode = (ValueNode) node; 355 if (trace) { 356 TTY.println("LIRGen for " + valueNode); 357 } 358 Value operand = getOperand(valueNode); 359 if (operand == null) { 360 if (!peephole(valueNode)) { 361 try { 362 doRoot(valueNode); 363 } catch (GraalError e) { 364 throw GraalGraphError.transformAndAddContext(e, valueNode); 365 } catch (Throwable e) { 366 throw new GraalGraphError(e).addContext(valueNode); 367 } 368 } 369 } else if (ComplexMatchValue.INTERIOR_MATCH.equals(operand)) { 370 // Doesn't need to be evaluated 371 Debug.log("interior match for %s", valueNode); 372 } else if (operand instanceof ComplexMatchValue) { 373 Debug.log("complex match for %s", valueNode); 374 ComplexMatchValue match = (ComplexMatchValue) operand; 375 operand = match.evaluate(this); 376 if (operand != null) { 377 setResult(valueNode, operand); 378 } 379 } else { 380 // There can be cases in which the result of an instruction is already set 381 // before by other instructions. 382 } 383 } 384 } 385 386 if (!gen.hasBlockEnd(block)) { 387 NodeIterable<Node> successors = block.getEndNode().successors(); 388 assert successors.count() == block.getSuccessorCount(); 389 if (block.getSuccessorCount() != 1) { 390 /* 391 * If we have more than one successor, we cannot just use the first one. Since 392 * successors are unordered, this would be a random choice. 393 */ 394 throw new GraalError("Block without BlockEndOp: " + block.getEndNode()); 395 } 396 gen.emitJump(getLIRBlock((FixedNode) successors.first())); 397 } 398 399 assert verifyBlock(gen.getResult().getLIR(), block); 400 } 401 } 402 403 @SuppressWarnings("try") 404 protected void matchComplexExpressions(List<Node> nodes) { 405 if (matchRules != null) { 406 try (Scope s = Debug.scope("MatchComplexExpressions")) { 407 if (LogVerbose.getValue(nodeOperands.graph().getOptions())) { 408 int i = 0; 409 for (Node node : nodes) { 410 Debug.log("%d: (%s) %1S", i++, node.getUsageCount(), node); 411 } 412 } 413 414 // Match the nodes in backwards order to encourage longer matches. 415 for (int index = nodes.size() - 1; index >= 0; index--) { 416 Node node = nodes.get(index); 417 if (getOperand(node) != null) { 418 continue; 419 } 420 // See if this node is the root of any MatchStatements 421 List<MatchStatement> statements = matchRules.get(node.getClass()); 422 if (statements != null) { 423 for (MatchStatement statement : statements) { 424 if (statement.generate(this, index, node, nodes)) { 425 // Found a match so skip to the next 426 break; 427 } 428 } 429 } 430 } 431 } 432 } 433 } 434 435 protected abstract boolean peephole(ValueNode valueNode); 436 437 private void doRoot(ValueNode instr) { 438 if (traceLIRGeneratorLevel >= 2) { 439 TTY.println("Emitting LIR for instruction " + instr); 440 } 441 currentInstruction = instr; 442 443 Debug.log("Visiting %s", instr); 444 emitNode(instr); 445 Debug.log("Operand for %s = %s", instr, getOperand(instr)); 446 } 447 448 protected void emitNode(ValueNode node) { 449 if (Debug.isLogEnabled() && node.stamp().isEmpty()) { 450 Debug.log("This node has an empty stamp, we are emitting dead code(?): %s", node); 451 } 452 setSourcePosition(node.getNodeSourcePosition()); 453 if (node instanceof LIRLowerable) { 454 ((LIRLowerable) node).generate(this); 455 } else { 456 throw GraalError.shouldNotReachHere("node is not LIRLowerable: " + node); 457 } 458 } 459 460 protected void emitPrologue(StructuredGraph graph) { 461 CallingConvention incomingArguments = gen.getResult().getCallingConvention(); 462 463 Value[] params = new Value[incomingArguments.getArgumentCount()]; 464 for (int i = 0; i < params.length; i++) { 465 params[i] = incomingArguments.getArgument(i); 466 if (ValueUtil.isStackSlot(params[i])) { 467 StackSlot slot = ValueUtil.asStackSlot(params[i]); 468 if (slot.isInCallerFrame() && !gen.getResult().getLIR().hasArgInCallerFrame()) { 469 gen.getResult().getLIR().setHasArgInCallerFrame(); 470 } 471 } 472 } 473 474 gen.emitIncomingValues(params); 475 476 for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) { 477 Value paramValue = params[param.index()]; 478 assert paramValue.getValueKind().equals(getLIRGeneratorTool().getLIRKind(param.stamp())) : paramValue + " " + getLIRGeneratorTool().getLIRKind(param.stamp()); 479 setResult(param, gen.emitMove(paramValue)); 480 } 481 } 482 483 @Override 484 public void visitMerge(AbstractMergeNode x) { 485 } 486 487 @Override 488 public void visitEndNode(AbstractEndNode end) { 489 AbstractMergeNode merge = end.merge(); 490 JumpOp jump = newJumpOp(getLIRBlock(merge)); 491 jump.setPhiValues(createPhiOut(merge, end)); 492 append(jump); 493 } 494 495 /** 496 * Runtime specific classes can override this to insert a safepoint at the end of a loop. 497 */ 498 @Override 499 public void visitLoopEnd(LoopEndNode x) { 500 } 501 502 protected JumpOp newJumpOp(LabelRef ref) { 503 return new JumpOp(ref); 504 } 505 506 protected LIRKind getPhiKind(PhiNode phi) { 507 return gen.getLIRKind(phi.stamp()); 508 } 509 510 @Override 511 public void emitIf(IfNode x) { 512 emitBranch(x.condition(), getLIRBlock(x.trueSuccessor()), getLIRBlock(x.falseSuccessor()), x.probability(x.trueSuccessor())); 513 } 514 515 public void emitBranch(LogicNode node, LabelRef trueSuccessor, LabelRef falseSuccessor, double trueSuccessorProbability) { 516 if (node instanceof IsNullNode) { 517 emitNullCheckBranch((IsNullNode) node, trueSuccessor, falseSuccessor, trueSuccessorProbability); 518 } else if (node instanceof CompareNode) { 519 emitCompareBranch((CompareNode) node, trueSuccessor, falseSuccessor, trueSuccessorProbability); 520 } else if (node instanceof LogicConstantNode) { 521 emitConstantBranch(((LogicConstantNode) node).getValue(), trueSuccessor, falseSuccessor); 522 } else if (node instanceof IntegerTestNode) { 523 emitIntegerTestBranch((IntegerTestNode) node, trueSuccessor, falseSuccessor, trueSuccessorProbability); 524 } else { 525 throw GraalError.unimplemented(node.toString()); 526 } 527 } 528 529 private void emitNullCheckBranch(IsNullNode node, LabelRef trueSuccessor, LabelRef falseSuccessor, double trueSuccessorProbability) { 530 LIRKind kind = gen.getLIRKind(node.getValue().stamp()); 531 Value nullValue = gen.emitConstant(kind, JavaConstant.NULL_POINTER); 532 gen.emitCompareBranch(kind.getPlatformKind(), operand(node.getValue()), nullValue, Condition.EQ, false, trueSuccessor, falseSuccessor, trueSuccessorProbability); 533 } 534 535 public void emitCompareBranch(CompareNode compare, LabelRef trueSuccessor, LabelRef falseSuccessor, double trueSuccessorProbability) { 536 PlatformKind kind = gen.getLIRKind(compare.getX().stamp()).getPlatformKind(); 537 gen.emitCompareBranch(kind, operand(compare.getX()), operand(compare.getY()), compare.condition(), compare.unorderedIsTrue(), trueSuccessor, falseSuccessor, trueSuccessorProbability); 538 } 539 540 public void emitIntegerTestBranch(IntegerTestNode test, LabelRef trueSuccessor, LabelRef falseSuccessor, double trueSuccessorProbability) { 541 gen.emitIntegerTestBranch(operand(test.getX()), operand(test.getY()), trueSuccessor, falseSuccessor, trueSuccessorProbability); 542 } 543 544 public void emitConstantBranch(boolean value, LabelRef trueSuccessorBlock, LabelRef falseSuccessorBlock) { 545 LabelRef block = value ? trueSuccessorBlock : falseSuccessorBlock; 546 gen.emitJump(block); 547 } 548 549 @Override 550 public void emitConditional(ConditionalNode conditional) { 551 Value tVal = operand(conditional.trueValue()); 552 Value fVal = operand(conditional.falseValue()); 553 setResult(conditional, emitConditional(conditional.condition(), tVal, fVal)); 554 } 555 556 public Variable emitConditional(LogicNode node, Value trueValue, Value falseValue) { 557 if (node instanceof IsNullNode) { 558 IsNullNode isNullNode = (IsNullNode) node; 559 LIRKind kind = gen.getLIRKind(isNullNode.getValue().stamp()); 560 Value nullValue = gen.emitConstant(kind, JavaConstant.NULL_POINTER); 561 return gen.emitConditionalMove(kind.getPlatformKind(), operand(isNullNode.getValue()), nullValue, Condition.EQ, false, trueValue, falseValue); 562 } else if (node instanceof CompareNode) { 563 CompareNode compare = (CompareNode) node; 564 PlatformKind kind = gen.getLIRKind(compare.getX().stamp()).getPlatformKind(); 565 return gen.emitConditionalMove(kind, operand(compare.getX()), operand(compare.getY()), compare.condition(), compare.unorderedIsTrue(), trueValue, falseValue); 566 } else if (node instanceof LogicConstantNode) { 567 return gen.emitMove(((LogicConstantNode) node).getValue() ? trueValue : falseValue); 568 } else if (node instanceof IntegerTestNode) { 569 IntegerTestNode test = (IntegerTestNode) node; 570 return gen.emitIntegerTestMove(operand(test.getX()), operand(test.getY()), trueValue, falseValue); 571 } else { 572 throw GraalError.unimplemented(node.toString()); 573 } 574 } 575 576 @Override 577 public void emitInvoke(Invoke x) { 578 LoweredCallTargetNode callTarget = (LoweredCallTargetNode) x.callTarget(); 579 CallingConvention invokeCc = gen.getResult().getFrameMapBuilder().getRegisterConfig().getCallingConvention(callTarget.callType(), x.asNode().stamp().javaType(gen.getMetaAccess()), 580 callTarget.signature(), gen); 581 gen.getResult().getFrameMapBuilder().callsMethod(invokeCc); 582 583 Value[] parameters = visitInvokeArguments(invokeCc, callTarget.arguments()); 584 585 LabelRef exceptionEdge = null; 586 if (x instanceof InvokeWithExceptionNode) { 587 exceptionEdge = getLIRBlock(((InvokeWithExceptionNode) x).exceptionEdge()); 588 } 589 LIRFrameState callState = stateWithExceptionEdge(x, exceptionEdge); 590 591 Value result = invokeCc.getReturn(); 592 if (callTarget instanceof DirectCallTargetNode) { 593 emitDirectCall((DirectCallTargetNode) callTarget, result, parameters, AllocatableValue.NONE, callState); 594 } else if (callTarget instanceof IndirectCallTargetNode) { 595 emitIndirectCall((IndirectCallTargetNode) callTarget, result, parameters, AllocatableValue.NONE, callState); 596 } else { 597 throw GraalError.shouldNotReachHere(); 598 } 599 600 if (isLegal(result)) { 601 setResult(x.asNode(), gen.emitMove(result)); 602 } 603 604 if (x instanceof InvokeWithExceptionNode) { 605 gen.emitJump(getLIRBlock(((InvokeWithExceptionNode) x).next())); 606 } 607 } 608 609 protected abstract void emitDirectCall(DirectCallTargetNode callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState callState); 610 611 protected abstract void emitIndirectCall(IndirectCallTargetNode callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState callState); 612 613 @Override 614 public Value[] visitInvokeArguments(CallingConvention invokeCc, Collection<ValueNode> arguments) { 615 // for each argument, load it into the correct location 616 Value[] result = new Value[arguments.size()]; 617 int j = 0; 618 for (ValueNode arg : arguments) { 619 if (arg != null) { 620 AllocatableValue operand = invokeCc.getArgument(j); 621 gen.emitMove(operand, operand(arg)); 622 result[j] = operand; 623 j++; 624 } else { 625 throw GraalError.shouldNotReachHere("I thought we no longer have null entries for two-slot types..."); 626 } 627 } 628 return result; 629 } 630 631 /** 632 * This method tries to create a switch implementation that is optimal for the given switch. It 633 * will either generate a sequential if/then/else cascade, a set of range tests or a table 634 * switch. 635 * 636 * If the given switch does not contain int keys, it will always create a sequential 637 * implementation. 638 */ 639 @Override 640 public void emitSwitch(SwitchNode x) { 641 assert x.defaultSuccessor() != null; 642 LabelRef defaultTarget = getLIRBlock(x.defaultSuccessor()); 643 int keyCount = x.keyCount(); 644 if (keyCount == 0) { 645 gen.emitJump(defaultTarget); 646 } else { 647 Variable value = gen.load(operand(x.value())); 648 if (keyCount == 1) { 649 assert defaultTarget != null; 650 double probability = x.probability(x.keySuccessor(0)); 651 LIRKind kind = gen.getLIRKind(x.value().stamp()); 652 Value key = gen.emitConstant(kind, x.keyAt(0)); 653 gen.emitCompareBranch(kind.getPlatformKind(), gen.load(operand(x.value())), key, Condition.EQ, false, getLIRBlock(x.keySuccessor(0)), defaultTarget, probability); 654 } else if (x instanceof IntegerSwitchNode && x.isSorted()) { 655 IntegerSwitchNode intSwitch = (IntegerSwitchNode) x; 656 LabelRef[] keyTargets = new LabelRef[keyCount]; 657 JavaConstant[] keyConstants = new JavaConstant[keyCount]; 658 double[] keyProbabilities = new double[keyCount]; 659 JavaKind keyKind = intSwitch.keyAt(0).getJavaKind(); 660 for (int i = 0; i < keyCount; i++) { 661 keyTargets[i] = getLIRBlock(intSwitch.keySuccessor(i)); 662 keyConstants[i] = intSwitch.keyAt(i); 663 keyProbabilities[i] = intSwitch.keyProbability(i); 664 assert keyConstants[i].getJavaKind() == keyKind; 665 } 666 gen.emitStrategySwitch(keyConstants, keyProbabilities, keyTargets, defaultTarget, value); 667 } else { 668 // keyKind != JavaKind.Int || !x.isSorted() 669 LabelRef[] keyTargets = new LabelRef[keyCount]; 670 Constant[] keyConstants = new Constant[keyCount]; 671 double[] keyProbabilities = new double[keyCount]; 672 for (int i = 0; i < keyCount; i++) { 673 keyTargets[i] = getLIRBlock(x.keySuccessor(i)); 674 keyConstants[i] = x.keyAt(i); 675 keyProbabilities[i] = x.keyProbability(i); 676 } 677 678 // hopefully only a few entries 679 gen.emitStrategySwitch(new SwitchStrategy.SequentialStrategy(keyProbabilities, keyConstants), value, keyTargets, defaultTarget); 680 } 681 } 682 } 683 684 public DebugInfoBuilder getDebugInfoBuilder() { 685 assert debugInfoBuilder != null; 686 return debugInfoBuilder; 687 } 688 689 private static FrameState getFrameState(DeoptimizingNode deopt) { 690 if (deopt instanceof DeoptimizingNode.DeoptBefore) { 691 assert !(deopt instanceof DeoptimizingNode.DeoptDuring || deopt instanceof DeoptimizingNode.DeoptAfter); 692 return ((DeoptimizingNode.DeoptBefore) deopt).stateBefore(); 693 } else if (deopt instanceof DeoptimizingNode.DeoptDuring) { 694 assert !(deopt instanceof DeoptimizingNode.DeoptAfter); 695 return ((DeoptimizingNode.DeoptDuring) deopt).stateDuring(); 696 } else { 697 assert deopt instanceof DeoptimizingNode.DeoptAfter; 698 return ((DeoptimizingNode.DeoptAfter) deopt).stateAfter(); 699 } 700 } 701 702 @Override 703 public LIRFrameState state(DeoptimizingNode deopt) { 704 if (!deopt.canDeoptimize()) { 705 return null; 706 } 707 return stateFor(getFrameState(deopt)); 708 } 709 710 public LIRFrameState stateWithExceptionEdge(DeoptimizingNode deopt, LabelRef exceptionEdge) { 711 if (!deopt.canDeoptimize()) { 712 return null; 713 } 714 return stateForWithExceptionEdge(getFrameState(deopt), exceptionEdge); 715 } 716 717 public LIRFrameState stateFor(FrameState state) { 718 return stateForWithExceptionEdge(state, null); 719 } 720 721 public LIRFrameState stateForWithExceptionEdge(FrameState state, LabelRef exceptionEdge) { 722 if (gen.needOnlyOopMaps()) { 723 return new LIRFrameState(null, null, null); 724 } 725 assert state != null; 726 return getDebugInfoBuilder().build(state, exceptionEdge); 727 } 728 729 @Override 730 public void emitOverflowCheckBranch(AbstractBeginNode overflowSuccessor, AbstractBeginNode next, Stamp stamp, double probability) { 731 LIRKind cmpKind = getLIRGeneratorTool().getLIRKind(stamp); 732 gen.emitOverflowCheckBranch(getLIRBlock(overflowSuccessor), getLIRBlock(next), cmpKind, probability); 733 } 734 735 @Override 736 public void visitFullInfopointNode(FullInfopointNode i) { 737 append(new FullInfopointOp(stateFor(i.getState()), i.getReason())); 738 } 739 740 @Override 741 public void setSourcePosition(NodeSourcePosition position) { 742 gen.setSourcePosition(position); 743 } 744 745 @Override 746 public LIRGeneratorTool getLIRGeneratorTool() { 747 return gen; 748 } 749 }