Example #1
0
        //--//

        public static bool IsAncestor(ITreeNode <FC> node,
                                      ITreeNode <FC> child)
        {
            while (child != null)
            {
                if (node == child)
                {
                    return(true);
                }

                ITreeNode <FC> nodeNext = null;

                foreach (ITreeEdge <FC> edge in child.Predecessors)
                {
                    if (edge.EdgeClass == EdgeClass.TreeEdge)
                    {
                        nodeNext = (ITreeNode <FC>)edge.Predecessor;
                        break;
                    }
                }

                CHECKS.ASSERT(nodeNext != null || child.Predecessors.Length == 0, "Child not a member of a spanning tree");

                child = nodeNext;
            }

            return(false);
        }
Example #2
0
        //--//

        public ExpressionTree(T v)
        {
            CHECKS.ASSERT(v != null, "Cannot insert a null node is an expression tree!");

            Left  = Right = null;
            Value = v;
        }
Example #3
0
        private void ComputeImmediatePostDominators( )
        {
            int len = m_nodes.Length;

            BitVector[] tmp = BitVector.AllocateBitVectors(len, len);

            for (int n = 0; n < len; n++)
            {
                // Tmp(n) := PostDomin(n) - {n}
                tmp[n].Assign(m_postDominance[n]);
                tmp[n].Clear(n);
            }

            for (int n = 0; n < len; n++)  // Walk the basic blocks in pre-order.
            {
                // for each n in N do

                BitVector tmpN = tmp[n];

                for (int s = 0; s < len; s++)
                {
                    if (tmpN[s])
                    {
                        // for each s in Tmp(n) do

                        BitVector tmpS = tmp[s];

                        for (int t = 0; t < len; t++)
                        {
                            if (t != s && tmpN[t])
                            {
                                // for each t in Tmp(n) - {s} do

                                if (tmpS[t])
                                {
                                    // if t in Tmp(s) then Tmp(n) -= {t}

                                    tmpN.Clear(t);
                                }
                            }
                        }
                    }
                }
            }

            m_immediatePostDominators = new N[len];

            for (int n = 0; n < len; n++)
            {
                bool fGot = false;

                foreach (int idom in tmp[n])
                {
                    CHECKS.ASSERT(fGot == false, "Internal failure, found more than one immediate post dominators");

                    m_immediatePostDominators[n] = m_nodes[idom];

                    fGot = true;
                }
            }
        }
Example #4
0
        //
        // Helper Methods
        //

        private void ComputeDominators( )
        {
            //
            // This is an implementation of the algorithm in "A Simple, Fast Dominance Algorithm", by Keith D. Cooper, Timothy J. Harvey, and Ken Kennedy.
            //
            //  "for all nodes, b /* initialize the dominators array */
            //      doms[b] = Undefined
            //  doms[start_node] = start_node
            //  Changed = true
            //  while (Changed)
            //      Changed = false
            //      for all nodes, b, in reverse postorder (except start node)
            //          new_idom = first (processed) predecessor of b /* (pick one) */
            //          for all other predecessors, p, of b
            //              if doms[p] != Undefined /* i.e., if doms[p] already calculated */
            //                  new_idom = intersect(p, new_idom)
            //          if doms[b] != new_idom
            //              doms[b] = new_idom
            //              Changed = true
            //
            //  function intersect(b1, b2) returns node
            //      finger1 = b1
            //      finger2 = b2
            //      while (finger1 != finger2)
            //          while (finger1 < finger2)
            //              finger1 = doms[finger1]
            //          while (finger2 < finger1)
            //              finger2 = doms[finger2]
            //      return finger1"
            //

            int num = m_nodesPostOrder.Length;

            m_immediateDominancePostOrder = new N[num];

            N startNode = m_nodesSpanningTree[0];

            SetIDom(startNode, startNode);

            while (true)
            {
                bool fChanged = false;

                for (int pos = num - 1; --pos >= 0;)
                {
                    N nodeB = m_nodesPostOrder[pos];

                    CHECKS.ASSERT(!(nodeB is IEntryTreeNode <FC>), "Start node should not be processed by ImmediateDominance algorithm");
                    CHECKS.ASSERT(nodeB.Predecessors.Length > 0, "Node 'b' should have a predecessor: {0}", nodeB);

                    N first = null;
                    foreach (ITreeEdge <FC> edge in nodeB.Predecessors)
                    {
                        N node = (N)edge.Predecessor;

                        if (GetIDom(node) != null)
                        {
                            first = node;
                            break;
                        }
                    }

                    CHECKS.ASSERT(first != null, "Cannot find first (processed) predecessor of 'b'");

                    N new_idom = first;

                    foreach (ITreeEdge <FC> edge in nodeB.Predecessors)
                    {
                        N nodeP = (N)edge.Predecessor;

                        if (nodeP != first)
                        {
                            if (GetIDom(nodeP) != null)
                            {
                                new_idom = Intersect(nodeP, new_idom);
                            }
                        }
                    }

                    if (GetIDom(nodeB) != new_idom)
                    {
                        SetIDom(nodeB, new_idom);

                        fChanged = true;
                    }
                }

                if (!fChanged)
                {
                    break;
                }
            }

            //--//

            //
            // Finally, convert to an array in spanning-tree order.
            //
            m_immediateDominanceSpanningTree = new N[num];
            for (int pos = 0; pos < num; pos++)
            {
                m_immediateDominanceSpanningTree[pos] = GetIDom(m_nodesSpanningTree[pos]);
            }
        }