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