001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.compilers.opt.util;
014
015
016/**
017 * SpaceEffGraphEdge is a generic graph edge.  Extend this to implement
018 * specific graph edge types, or use it as a generic edge.
019 * SpaceEffGraphEdges are directed, and therefore, have a from-node and
020 * a to-node.
021 */
022public class SpaceEffGraphEdge implements GraphEdge {
023  /**
024   * End node.
025   */
026  protected SpaceEffGraphNode _toNode;
027
028  /**
029   * Start node.
030   */
031  protected SpaceEffGraphNode _fromNode;
032
033  /**
034   * The following word is defined for several uses.  The first 4 bits
035   * are reserved for SpaceEffGraph.  Classes that subclass this one
036   * can use the remaining 28 bits
037   */
038  protected int flags;
039
040  static final int VISITED = 0x10000000; // general purpose
041
042  static final int BACK_EDGE = 0x20000000; // edge information
043  static final int DOMINATOR = 0x40000000; // edge information
044
045  static final int INFO_MASK = 0x0fffffff;
046
047  public final boolean visited() {
048    return (flags & VISITED) != 0;
049  }
050
051  public final boolean backEdge() {
052    return (flags & BACK_EDGE) != 0;
053  }
054
055  public final boolean dominatorEdge() {
056    return (flags & DOMINATOR) != 0;
057  }
058
059  public final void setVisited() {
060    flags |= VISITED;
061  }
062
063  public final void setBackEdge() {
064    flags |= BACK_EDGE;
065  }
066
067  public final void setDominatorEdge() {
068    flags |= DOMINATOR;
069  }
070
071  public final void clearVisited() {
072    flags &= ~VISITED;
073  }
074
075  public final void clearBackEdge() {
076    flags &= ~BACK_EDGE;
077  }
078
079  public final void clearDominatorEdge() {
080    flags &= ~DOMINATOR;
081  }
082
083  public final int getInfo() {
084    return flags & INFO_MASK;
085  }
086
087  public final void setInfo(int value) {
088    flags = (flags & ~INFO_MASK) | (value & INFO_MASK);
089  }
090
091  /**
092   * Get the end node for the edge.
093   * @return end node for the edge
094   */
095  public final SpaceEffGraphNode toNode() {
096    return _toNode;
097  }
098
099  /**
100   * Get the start node for the edge.
101   * @return start node for the edge
102   */
103  public final SpaceEffGraphNode fromNode() {
104    return _fromNode;
105  }
106
107  /**
108   * Set end node.
109   * WARNING: use with caution
110   * @param toNode new end node
111   */
112  final void setToNode(SpaceEffGraphNode toNode) {
113    _toNode = toNode;
114  }
115
116  /**
117   * Set start node.
118   * WARNING: use with caution
119   * @param fromNode new start node
120   */
121  final void setFromNode(SpaceEffGraphNode fromNode) {
122    _fromNode = fromNode;
123  }
124
125  /**
126   * Constructs an empty edge.
127   */
128  SpaceEffGraphEdge() {
129    // explicitly defined to get package-private constructor
130  }
131
132  /**
133   * Constructs an edge starting at a given node and ending at a given node.
134   * @param fromNode start node
135   * @param toNode end node
136   */
137  protected SpaceEffGraphEdge(SpaceEffGraphNode fromNode, SpaceEffGraphNode toNode) {
138    _toNode = toNode;
139    _fromNode = fromNode;
140  }
141
142  /**
143   * Delete this edge from the graph.
144   */
145  final void delete() {
146    _fromNode.removeOut(this);
147    _toNode.removeIn(this);
148  }
149
150  /**
151   * Returns the string representation of the edge type.
152   * @return string representation of the edge type
153   */
154  public String getTypeString() {
155    return "";
156  }
157
158  /**
159   * Returns the string representation of the end node (used for printing).
160   * @return string representation of the end node
161   */
162  public String toNodeString() {
163    return "---> " + _toNode;
164  }
165
166  /**
167   * Returns the string representation of the start node (used for printing).
168   * @return string representation of the start node
169   */
170  public String fromNodeString() {
171    return "<--- " + _fromNode;
172  }
173
174  /**
175   * Get the end node for the edge.
176   * @return end node for the edge
177   */
178  @Override
179  public final GraphNode to() {
180    return _toNode;
181  }
182
183  /**
184   * Get the start node for the edge.
185   * @return start node for the edge
186   */
187  @Override
188  public final GraphNode from() {
189    return _fromNode;
190  }
191
192  /**
193   * Links inlined from LinkedListElement2.
194   */
195  protected SpaceEffGraphEdge nextIn, nextOut;
196
197  /**
198   * Get the next in edge.
199   * @return next in edge.
200   */
201  public final SpaceEffGraphEdge getNextIn() {
202    return nextIn;
203  }
204
205  /**
206   * Get the next out edge.
207   * @return next out edge.
208   */
209  public final SpaceEffGraphEdge getNextOut() {
210    return nextOut;
211  }
212
213  /**
214   * Append a given edge after this edge as an in edge.
215   * @param e the edge to append
216   */
217  final void appendIn(SpaceEffGraphEdge e) {
218    nextIn = e;
219  }
220
221  /**
222   * Append a given edge after this edge as an out edge.
223   * @param e the edge to append
224   */
225  final void appendOut(SpaceEffGraphEdge e) {
226    nextOut = e;
227  }
228}
229