C-Breeze
C Compiler Infrastructure

[ Project home page]
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

dominancefrontiers.cc

Go to the documentation of this file.
00001 // $Id: dominancefrontiers.cc,v 1.6 2003/08/07 23:14:13 pnav Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 // 
00009 //  Samuel Z. Guyer
00010 //  Daniel A. Jimenez
00011 //  Calvin Lin
00012 // 
00013 //  Permission is hereby granted, free of charge, to any person
00014 //  obtaining a copy of this software and associated documentation
00015 //  files (the "Software"), to deal in the Software without
00016 //  restriction, including without limitation the rights to use, copy,
00017 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00018 //  of the Software, and to permit persons to whom the Software is
00019 //  furnished to do so, subject to the following conditions:
00020 //  
00021 //  The above copyright notice and this permission notice shall be
00022 //  included in all copies or substantial portions of the Software.
00023 //  
00024 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00028 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00029 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00030 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 //  THE SOFTWARE.
00032 //
00033 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00034 //  Computer Science for inspiring parts of the C-Breeze design.
00035 //
00036 // ----------------------------------------------------------------------
00037 
00038 #include "c_breeze.h"
00039 #include "dominancefrontiers.h"
00040 
00041 // ------------------------------------------------------------
00042 // Constructor
00043 // ------------------------------------------------------------
00044 
00045 DominanceFrontiers::DominanceFrontiers(procNode * proc)
00046   : basicblock_map(),
00047     _proc(proc),
00048     _root(0),
00049     df_vec(),
00050     _index(0)
00051 {
00052   _root = proc->entry();
00053   assert(_root->typ() == Block);
00054 
00055   Dominators dom(proc, true);
00056 
00057   depth_first_search(_root);
00058 
00059   compute_dominance_frontiers();
00060 }
00061 
00062 // --- Compute a depth-first ordering of the basic blocks. Store that
00063 // information on each basic block in "dfn".
00064 
00065 void DominanceFrontiers::depth_first_search(basicblockNode * node)
00066 {
00067   if (std::find(df_vec.begin(), df_vec.end(), node) == df_vec.end()) {
00068     
00069     node->dfn(_index);
00070     df_vec.push_back(node);
00071 
00072     ++_index;
00073 
00074     // --- Check the ordering:
00075     //       _forward = true  --> successors   --> dominators
00076     //       _forward = false --> predecessors --> post-dominators
00077 
00078     basicblock_list & bbl = node->succs();
00079 
00080     for (basicblock_list_p p = bbl.begin();
00081          p != bbl.end();
00082          ++p)
00083       {
00084         basicblockNode * curblock = *p;
00085         depth_first_search(curblock);
00086       }
00087   }
00088 }
00089 
00090 // ------------------------------------------------------------
00091 // Build dominance frontiers
00092 // ------------------------------------------------------------
00093 
00094 void DominanceFrontiers::compute_dominance_frontiers()
00095 {
00096   basicblockset_map DF;
00097 
00098   int num_nodes = df_vec.size();
00099 
00100   for (int i = num_nodes - 1; i >= 0; i--) {
00101     basicblockNode * X = df_vec[i];
00102 
00103     DF[X] = basicblock_bitset();
00104 
00105     basicblock_list & bbl = X->succs();
00106 
00107     // --- Compute DF_local: any immediate successors Y that are not
00108     // dominated by X, or if Y and X are the same node.
00109 
00110     for (basicblock_list_p p = bbl.begin();
00111          p != bbl.end();
00112          ++p)
00113       {
00114         basicblockNode * Y = *p;
00115 
00116         if ((X == Y) ||
00117             ( ! Dominators::dominates(X, Y))) {
00118           DF[X].set(Y->dfn());
00119         }
00120       }
00121 
00122     // --- Compute DF_up: any nodes in the dominance frontier of Xs
00123     // children, unless X happens to dominate them.
00124 
00125     for (basicblock_list_p p = X->children().begin();
00126          p != X->children().end();
00127          ++p)
00128       {
00129         basicblockNode * Z = *p;
00130 
00131         for (int j = 0; j < num_nodes; j++)
00132           if (DF[Z].test(j)) {
00133             basicblockNode * Y = df_vec[j];
00134             if ( ! Dominators::dominates(X, Y) )
00135               DF[X].set(Y->dfn());
00136           }
00137       }
00138 
00139     // ostringstream ost;
00140     // ost << "DF[" << X->dfn() << "] = ";
00141     // output_context oc(cout);
00142     // if (! X)
00143     //  cout << "X is null" << endl;
00144     // else
00145     //  X->output(oc,0);
00146     // for (int j = 0; j < num_nodes; j++)
00147     //  if (DF[X].test(j))
00148     //   ost << j << " ";
00149     // X->comment() = ost.str();
00150   }
00151 
00152   // --- Convert bitset representation into the list-oriented
00153   // representation.
00154 
00155   for (int i = 0; i < (int)df_vec.size(); ++i) {
00156 
00157     // --- Get the current basic block
00158 
00159     basicblockNode * cur = df_vec[i];
00160 
00161     // --- Get the bit-vector representation of its DF
00162 
00163     basicblock_bitset & DFblock = DF[cur];
00164 
00165     // --- Add an empty list into "this" for the block
00166 
00167     pair<basicblockNode *, basicblock_list> el(cur, basicblock_list());
00168     pair<basicblock_map_p, bool> result = insert(el);
00169 
00170     // --- Get a reference to that list
00171 
00172     basicblock_list & bbl = ( * (result.first)).second;
00173 
00174     // --- Translate each bit into a basicblockNode and insert in the
00175     // list.
00176 
00177     for (int j = 0; j < num_nodes; j++)
00178       if (DFblock.test(j)) {
00179         basicblockNode * Y = df_vec[j];
00180         bbl.push_back(Y);
00181       }
00182   }
00183 }

Generated on August 27, 2003
Back to the C-Breeze home page