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.ssa;
014
015import org.jikesrvm.compilers.opt.ir.Instruction;
016import org.jikesrvm.compilers.opt.ir.operand.ConstantOperand;
017import org.jikesrvm.compilers.opt.util.SpaceEffGraphNode;
018
019/**
020 * This class implements a vertex in the value graph used in global
021 * value numbering
022 * ala Alpern, Wegman and Zadeck.  See Muchnick p.348 for a nice
023 * discussion.
024 */
025final class ValueGraphVertex extends SpaceEffGraphNode {
026  /** the name of the variable defined by this node */
027  private final Object name;
028  /** the name of the operator that does the definition */
029  private Object label;
030  /** operand vertices, in order */
031  private ValueGraphVertex[] targets;
032  /** integer value number */
033  private int valueNumber;
034  /** number of operands needed */
035  private int arity;
036
037  ValueGraphVertex(Object name) {
038    this.name = name;
039  }
040
041  /**
042   * Sets up properties of this vertex identically to another vertex.
043   *
044   * @param v the vertex to copy the properties from
045   */
046  void copyVertex(ValueGraphVertex v) {
047    this.label = v.label;
048    this.valueNumber = v.valueNumber;
049    this.arity = v.arity;
050    this.targets = new ValueGraphVertex[v.targets.length];
051    for (int i = 0; i < targets.length; i++) {
052      this.targets[i] = v.targets[i];
053    }
054  }
055
056  /**
057   * @return whether this vertex represents an incoming parameter
058   */
059  boolean representsParameter() {
060    return (label instanceof ValueGraphParamLabel);
061  }
062
063  /**
064   * Set the label for this vertex.
065   *
066   * @param label the label (an operator of some type)
067   * @param arity the number of operands needed
068   */
069  void setLabel(Object label, int arity) {
070    this.label = label;
071    this.arity = arity;
072    targets = new ValueGraphVertex[arity];
073  }
074
075  Object getLabel() {
076    return label;
077  }
078
079  Object getName() {
080    return name;
081  }
082
083  int getValueNumber() {
084    return valueNumber;
085  }
086
087  void setValueNumber(int number) {
088    valueNumber = number;
089  }
090
091  boolean isConstant() {
092    return (label instanceof ConstantOperand);
093  }
094
095  // is the def for this node an allocation instruction?
096  boolean isBornAtAllocation() {
097    return (label instanceof Instruction);
098  }
099
100  /**
101   * @param i operand number
102   * @return the target of the ith operand of this node
103   */
104  public ValueGraphVertex getTarget(int i) {
105    return targets[i];
106  }
107
108  public void addTarget(ValueGraphVertex target, int pos) {
109    targets[pos] = target;
110  }
111
112  public int getArity() {
113    return arity;
114  }
115
116  @Override
117  public String toString() {
118    StringBuilder s = new StringBuilder("Vertex: " + name + " " + label);
119    s.append(" Targets: ");
120    for (int i = 0; i < arity; i++) {
121      if (targets[i] == null) {
122        s.append("null  ");
123      } else {
124        s.append(targets[i].getName()).append("  ");
125      }
126    }
127    return s.toString();
128  }
129}