1 /* 2 * Copyright (c) 2012, 2015, 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 24 25 package org.graalvm.compiler.loop; 26 27 import static org.graalvm.compiler.loop.MathUtil.mul; 28 29 import org.graalvm.compiler.core.common.type.IntegerStamp; 30 import org.graalvm.compiler.core.common.type.Stamp; 31 import org.graalvm.compiler.nodes.ConstantNode; 32 import org.graalvm.compiler.nodes.NodeView; 33 import org.graalvm.compiler.nodes.ValueNode; 34 import org.graalvm.compiler.nodes.calc.IntegerConvertNode; 35 import org.graalvm.compiler.nodes.calc.NegateNode; 36 import org.graalvm.compiler.nodes.util.GraphUtil; 37 38 public class DerivedScaledInductionVariable extends DerivedInductionVariable { 39 40 private final ValueNode scale; 41 private final ValueNode value; 42 43 public DerivedScaledInductionVariable(LoopEx loop, InductionVariable base, ValueNode scale, ValueNode value) { 44 super(loop, base); 45 this.scale = scale; 46 this.value = value; 47 } 48 49 public DerivedScaledInductionVariable(LoopEx loop, InductionVariable base, NegateNode value) { 50 super(loop, base); 51 this.scale = ConstantNode.forIntegerStamp(value.stamp(NodeView.DEFAULT), -1, value.graph()); 52 this.value = value; 53 } 54 55 public ValueNode getScale() { 56 return scale; 57 } 58 59 @Override 60 public ValueNode valueNode() { 61 return value; 62 } 63 64 @Override 65 public Direction direction() { 66 Stamp stamp = scale.stamp(NodeView.DEFAULT); 67 if (stamp instanceof IntegerStamp) { 68 IntegerStamp integerStamp = (IntegerStamp) stamp; 69 if (integerStamp.isStrictlyPositive()) { 70 return base.direction(); 71 } else if (integerStamp.isStrictlyNegative()) { 72 return base.direction().opposite(); 73 } 74 } 75 return null; 76 } 77 78 @Override 79 public ValueNode initNode() { 80 return mul(graph(), base.initNode(), scale); 81 } 82 83 @Override 84 public ValueNode strideNode() { 85 return mul(graph(), base.strideNode(), scale); 86 } 87 88 @Override 89 public boolean isConstantInit() { 90 return scale.isConstant() && base.isConstantInit(); 91 } 92 93 @Override 94 public boolean isConstantStride() { 95 return scale.isConstant() && base.isConstantStride(); 96 } 97 98 @Override 99 public long constantInit() { 100 return base.constantInit() * scale.asJavaConstant().asLong(); 101 } 102 103 @Override 104 public long constantStride() { 105 return base.constantStride() * scale.asJavaConstant().asLong(); 106 } 107 108 @Override 109 public ValueNode extremumNode(boolean assumePositiveTripCount, Stamp stamp) { 110 return mul(graph(), base.extremumNode(assumePositiveTripCount, stamp), IntegerConvertNode.convert(scale, stamp, graph(), NodeView.DEFAULT)); 111 } 112 113 @Override 114 public ValueNode exitValueNode() { 115 return mul(graph(), base.exitValueNode(), scale); 116 } 117 118 @Override 119 public boolean isConstantExtremum() { 120 return scale.isConstant() && base.isConstantExtremum(); 121 } 122 123 @Override 124 public long constantExtremum() { 125 return base.constantExtremum() * scale.asJavaConstant().asLong(); 126 } 127 128 @Override 129 public void deleteUnusedNodes() { 130 GraphUtil.tryKillUnused(scale); 131 } 132 133 @Override 134 public String toString() { 135 return String.format("DerivedScaleInductionVariable base (%s) %s %s", base, value.getNodeClass().shortName(), scale); 136 } 137 }