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.depgraph;
014
015import static org.jikesrvm.compilers.opt.depgraph.DepGraphConstants.COMPACT;
016import static org.jikesrvm.compilers.opt.depgraph.DepGraphConstants.REG_TRUE;
017
018import org.jikesrvm.compilers.opt.ir.Instruction;
019import org.jikesrvm.compilers.opt.ir.operand.RegisterOperand;
020import org.jikesrvm.compilers.opt.util.SpaceEffGraphNode;
021
022/**
023 * Dependence graph node: there is one for each instruction in a basic block.
024 */
025public class DepGraphNode extends SpaceEffGraphNode {
026
027  /**
028   * Instruction that this node represents.
029   */
030  public final Instruction _instr;
031
032  /**
033   * Constructor.
034   * @param instr the instruction this node represents
035   */
036  public DepGraphNode(Instruction instr) {
037    _instr = instr;
038  }
039
040  /**
041   * Get the instruction this node represents.
042   * @return instruction this node represents
043   */
044  public Instruction instruction() {
045    return _instr;
046  }
047
048  /**
049   * Returns the string representation of this node.
050   * @return string representation of this node
051   */
052  @Override
053  public String toString() {
054    return "[" + _instr + "]";
055  }
056
057  /**
058   * Add an out edge from this node to the given node.
059   * @param node destination node for the edge
060   * @param type the type of the edge to add
061   */
062  public void insertOutEdge(DepGraphNode node, int type) {
063    if (COMPACT) {
064      int numTries = 0; // bound to avoid quadratic blowup.
065      for (DepGraphEdge oe = (DepGraphEdge) firstOutEdge(); oe != null && numTries < 4; oe =
066          (DepGraphEdge) oe.getNextOut(), numTries++) {
067        if (oe.toNode() == node) {
068          oe.addDepType(type);
069          return;
070        }
071      }
072    }
073    DepGraphEdge edge = new DepGraphEdge(this, node, type);
074    this.appendOutEdge(edge);
075    node.appendInEdge(edge);
076  }
077
078  /**
079   * Add an out edge this node to the given node
080   * because of a register true dependence of a given operand.
081   * @param node destination node for the edge
082   * @param op   the operand of node that is defined by this edge
083   */
084  public void insertRegTrueOutEdge(DepGraphNode node, RegisterOperand op) {
085    DepGraphEdge e = new DepGraphEdge(op, this, node, REG_TRUE);
086    this.appendOutEdge(e);
087    node.appendInEdge(e);
088  }
089}