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      @Override
050      public String toString() {
051        return "[" + _instr + "]";
052      }
053    
054      /**
055       * Add an out edge from this node to the given node.
056       * @param node destination node for the edge
057       * @param type the type of the edge to add
058       */
059      public void insertOutEdge(DepGraphNode node, int type) {
060        if (COMPACT) {
061          int numTries = 0; // bound to avoid quadratic blowup.
062          for (DepGraphEdge oe = (DepGraphEdge) firstOutEdge(); oe != null && numTries < 4; oe =
063              (DepGraphEdge) oe.getNextOut(), numTries++) {
064            if (oe.toNode() == node) {
065              oe.addDepType(type);
066              return;
067            }
068          }
069        }
070        DepGraphEdge edge = new DepGraphEdge(this, node, type);
071        this.appendOutEdge(edge);
072        node.appendInEdge(edge);
073      }
074    
075      /**
076       * Add an out edge this node to the given node
077       * because of a register true dependence of a given operand.
078       * @param node destination node for the edge
079       * @param op   the operand of node that is defined by this edge
080       */
081      public void insertRegTrueOutEdge(DepGraphNode node, RegisterOperand op) {
082        DepGraphEdge e = new DepGraphEdge(op, this, node, REG_TRUE);
083        this.appendOutEdge(e);
084        node.appendInEdge(e);
085      }
086    }