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