1 /* 2 * Copyright (c) 2014, 2014, 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.nodes.calc; 24 25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_5; 26 27 import java.util.EnumMap; 28 29 import org.graalvm.compiler.core.common.calc.FloatConvert; 30 import org.graalvm.compiler.core.common.type.ArithmeticOpTable; 31 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.FloatConvertOp; 32 import org.graalvm.compiler.graph.NodeClass; 33 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 34 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool; 35 import org.graalvm.compiler.nodeinfo.NodeInfo; 36 import org.graalvm.compiler.nodes.ValueNode; 37 import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable; 38 import org.graalvm.compiler.nodes.spi.Lowerable; 39 import org.graalvm.compiler.nodes.spi.LoweringTool; 40 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 41 42 import jdk.vm.ci.meta.Constant; 43 import jdk.vm.ci.meta.ConstantReflectionProvider; 44 45 /** 46 * A {@code FloatConvert} converts between integers and floating point numbers according to Java 47 * semantics. 48 */ 49 @NodeInfo(cycles = CYCLES_5) 50 public final class FloatConvertNode extends UnaryArithmeticNode<FloatConvertOp> implements ConvertNode, Lowerable, ArithmeticLIRLowerable { 51 public static final NodeClass<FloatConvertNode> TYPE = NodeClass.create(FloatConvertNode.class); 52 53 protected final FloatConvert op; 54 55 private static final EnumMap<FloatConvert, SerializableUnaryFunction<FloatConvertOp>> getOps; 56 static { 57 getOps = new EnumMap<>(FloatConvert.class); 58 for (FloatConvert op : FloatConvert.values()) { 59 getOps.put(op, table -> table.getFloatConvert(op)); 60 } 61 } 62 63 public FloatConvertNode(FloatConvert op, ValueNode input) { 64 super(TYPE, getOps.get(op), input); 65 this.op = op; 66 } 67 68 public static ValueNode create(FloatConvert op, ValueNode input) { 69 ValueNode synonym = findSynonym(input, ArithmeticOpTable.forStamp(input.stamp()).getFloatConvert(op)); 70 if (synonym != null) { 71 return synonym; 72 } 73 return new FloatConvertNode(op, input); 74 } 75 76 public FloatConvert getFloatConvert() { 77 return op; 78 } 79 80 @Override 81 public Constant convert(Constant c, ConstantReflectionProvider constantReflection) { 82 return getArithmeticOp().foldConstant(c); 83 } 84 85 @Override 86 public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) { 87 FloatConvertOp reverse = ArithmeticOpTable.forStamp(stamp()).getFloatConvert(op.reverse()); 88 return reverse.foldConstant(c); 89 } 90 91 @Override 92 public boolean isLossless() { 93 switch (getFloatConvert()) { 94 case F2D: 95 case I2D: 96 return true; 97 default: 98 return false; 99 } 100 } 101 102 @Override 103 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { 104 ValueNode ret = super.canonical(tool, forValue); 105 if (ret != this) { 106 return ret; 107 } 108 109 if (forValue instanceof FloatConvertNode) { 110 FloatConvertNode other = (FloatConvertNode) forValue; 111 if (other.isLossless() && other.op == this.op.reverse()) { 112 return other.getValue(); 113 } 114 } 115 return this; 116 } 117 118 @Override 119 public void lower(LoweringTool tool) { 120 tool.getLowerer().lower(this, tool); 121 } 122 123 @Override 124 public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) { 125 nodeValueMap.setResult(this, gen.emitFloatConvert(getFloatConvert(), nodeValueMap.operand(getValue()))); 126 } 127 }