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