1 /*
   2  * Copyright (c) 2012, 2012, 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 org.graalvm.compiler.core.common.type.Stamp;
  26 import org.graalvm.compiler.debug.GraalError;
  27 import org.graalvm.compiler.nodes.StructuredGraph;
  28 import org.graalvm.compiler.nodes.ValueNode;
  29 
  30 /**
  31  * This class describes a value node that is an induction variable in a counted loop.
  32  */
  33 public abstract class InductionVariable {
  34 
  35     public enum Direction {
  36         Up,
  37         Down;
  38 
  39         public Direction opposite() {
  40             switch (this) {
  41                 case Up:
  42                     return Down;
  43                 case Down:
  44                     return Up;
  45                 default:
  46                     throw GraalError.shouldNotReachHere();
  47             }
  48         }
  49     }
  50 
  51     public abstract StructuredGraph graph();
  52 
  53     protected final LoopEx loop;
  54 
  55     public InductionVariable(LoopEx loop) {
  56         this.loop = loop;
  57     }
  58 
  59     public LoopEx getLoop() {
  60         return loop;
  61     }
  62 
  63     public abstract Direction direction();
  64 
  65     /**
  66      * Returns the value node that is described by this induction variable.
  67      */
  68     public abstract ValueNode valueNode();
  69 
  70     /**
  71      * Returns the node that gives the initial value of this induction variable.
  72      */
  73     public abstract ValueNode initNode();
  74 
  75     /**
  76      * Returns the stride of the induction variable. The stride is the value that is added to the
  77      * induction variable at each iteration.
  78      */
  79     public abstract ValueNode strideNode();
  80 
  81     public abstract boolean isConstantInit();
  82 
  83     public abstract boolean isConstantStride();
  84 
  85     public abstract long constantInit();
  86 
  87     public abstract long constantStride();
  88 
  89     /**
  90      * Returns the extremum value of the induction variable. The extremum value is the value of the
  91      * induction variable in the loop body of the last iteration, only taking into account the main
  92      * loop limit test. It's possible for the loop to exit before this value if
  93      * {@link CountedLoopInfo#isExactTripCount()} returns false for the containing loop.
  94      */
  95     public ValueNode extremumNode() {
  96         return extremumNode(false, valueNode().stamp());
  97     }
  98 
  99     public abstract ValueNode extremumNode(boolean assumePositiveTripCount, Stamp stamp);
 100 
 101     public abstract boolean isConstantExtremum();
 102 
 103     public abstract long constantExtremum();
 104 
 105     /**
 106      * Returns the exit value of the induction variable. The exit value is the value of the
 107      * induction variable at the loop exit.
 108      */
 109     public abstract ValueNode exitValueNode();
 110 
 111     /**
 112      * Deletes any nodes created within the scope of this object that have no usages.
 113      */
 114     public abstract void deleteUnusedNodes();
 115 }