1 /*
   2  * Copyright (c) 2012, 2018, 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         Direction baseDirection = base.direction();
  67         if (baseDirection == null) {
  68             return null;
  69         }
  70         Stamp stamp = scale.stamp(NodeView.DEFAULT);
  71         if (stamp instanceof IntegerStamp) {
  72             IntegerStamp integerStamp = (IntegerStamp) stamp;
  73             if (integerStamp.isStrictlyPositive()) {
  74                 return baseDirection;
  75             } else if (integerStamp.isStrictlyNegative()) {
  76                 return baseDirection.opposite();
  77             }
  78         }
  79         return null;
  80     }
  81 
  82     @Override
  83     public ValueNode initNode() {
  84         return mul(graph(), base.initNode(), scale);
  85     }
  86 
  87     @Override
  88     public ValueNode strideNode() {
  89         return mul(graph(), base.strideNode(), scale);
  90     }
  91 
  92     @Override
  93     public boolean isConstantInit() {
  94         return scale.isConstant() && base.isConstantInit();
  95     }
  96 
  97     @Override
  98     public boolean isConstantStride() {
  99         return scale.isConstant() && base.isConstantStride();
 100     }
 101 
 102     @Override
 103     public long constantInit() {
 104         return base.constantInit() * scale.asJavaConstant().asLong();
 105     }
 106 
 107     @Override
 108     public long constantStride() {
 109         return base.constantStride() * scale.asJavaConstant().asLong();
 110     }
 111 
 112     @Override
 113     public ValueNode extremumNode(boolean assumePositiveTripCount, Stamp stamp) {
 114         return mul(graph(), base.extremumNode(assumePositiveTripCount, stamp), IntegerConvertNode.convert(scale, stamp, graph(), NodeView.DEFAULT));
 115     }
 116 
 117     @Override
 118     public ValueNode exitValueNode() {
 119         return mul(graph(), base.exitValueNode(), scale);
 120     }
 121 
 122     @Override
 123     public boolean isConstantExtremum() {
 124         return scale.isConstant() && base.isConstantExtremum();
 125     }
 126 
 127     @Override
 128     public long constantExtremum() {
 129         return base.constantExtremum() * scale.asJavaConstant().asLong();
 130     }
 131 
 132     @Override
 133     public void deleteUnusedNodes() {
 134         GraphUtil.tryKillUnused(scale);
 135     }
 136 
 137     @Override
 138     public String toString() {
 139         return String.format("DerivedScaleInductionVariable base (%s) %s %s", base, value.getNodeClass().shortName(), scale);
 140     }
 141 }