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  

dfpreds.cc

Go to the documentation of this file.
00001 // $Id: dfpreds.cc,v 1.4 2003/08/07 23:14:22 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 "dfpreds.h"
00040 
00041 // ------------------------------------------------------------
00042 // Constructor
00043 // ------------------------------------------------------------
00044 
00045 DFPreds::DFPreds(procNode * proc)
00046   : basicblock_set_map_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   // cout << "Compute DF for " << proc->decl()->name() << endl;
00056 
00057   Dominators dom(proc, true);
00058 
00059   depth_first_search(_root);
00060 
00061   compute_dominance_frontiers();
00062 }
00063 
00064 // --- Compute a depth-first ordering of the basic blocks. Store that
00065 // information on each basic block in "dfn".
00066 
00067 void DFPreds::depth_first_search(basicblockNode * node)
00068 {
00069   if (std::find(df_vec.begin(), df_vec.end(), node) == df_vec.end()) {
00070     
00071     node->dfn(_index);
00072     df_vec.push_back(node);
00073 
00074     ++_index;
00075 
00076     // --- Check the ordering:
00077     //       _forward = true  --> successors   --> dominators
00078     //       _forward = false --> predecessors --> post-dominators
00079 
00080     basicblock_list & bbl = node->succs();
00081 
00082     for (basicblock_list_p p = bbl.begin();
00083          p != bbl.end();
00084          ++p)
00085       {
00086         basicblockNode * curblock = *p;
00087         depth_first_search(curblock);
00088       }
00089   }
00090 }
00091 
00092 // ------------------------------------------------------------
00093 // Build dominance frontiers
00094 // ------------------------------------------------------------
00095 
00096 void DFPreds::compute_dominance_frontiers()
00097 {
00098   basicblock_set_map_map & DF = *this;
00099 
00100   int num_nodes = df_vec.size();
00101 
00102   for (int i = num_nodes - 1; i >= 0; i--) {
00103     basicblockNode * X = df_vec[i];
00104 
00105     DF[X] = basicblock_set_map();
00106 
00107     basicblock_list & bbl = X->succs();
00108 
00109     // --- Compute DF_local: any immediate successors Y that are not
00110     // dominated by X, or if Y and X are the same node.
00111 
00112     // cout << "  Compute DF_local for BB " << X->dfn() << endl;
00113 
00114     for (basicblock_list_p p = bbl.begin();
00115          p != bbl.end();
00116          ++p)
00117       {
00118         basicblockNode * Y = *p;
00119 
00120         if ((X == Y) ||
00121             ( ! Dominators::dominates(X, Y))) {
00122           DF[X][Y] = basicblock_set();
00123           DF[X][Y].insert(X);
00124           // cout << "    add " << Y->dfn() << endl;
00125         }
00126       }
00127 
00128     // --- Compute DF_up: any nodes in the dominance frontier of Xs
00129     // children, unless X happens to dominate them.
00130 
00131     // For all children of X
00132     //cout << "  Compute DF_up..." << endl;
00133 
00134     for (basicblock_list_p p = X->children().begin();
00135          p != X->children().end();
00136          ++p)
00137       {
00138         basicblockNode * childX = *p;
00139 
00140         // For all elements of DF child
00141 
00142         for (basicblock_set_map_p q = DF[childX].begin();
00143              q != DF[childX].end();
00144              ++q)
00145           {
00146             basicblockNode * dfchildX = (*q).first;
00147             basicblock_set & preds_of_dfchildX = (*q).second;
00148 
00149             if ((X == dfchildX) ||
00150                 (! Dominators::dominates(X, dfchildX))) {
00151 
00152               // Found a DF element for X -- see if it's already there
00153               // or not. This affects how we update the predecessor
00154               // set.
00155 
00156               // cout << "    add " << dfchildX->dfn() << endl;
00157 
00158               basicblock_set_map_p w = DF[X].find(dfchildX);
00159               if (w == DF[X].end())
00160                 DF[X][dfchildX] = preds_of_dfchildX;
00161               else {
00162 
00163                 // Merge in the preds from the new DF element
00164 
00165                 for (basicblock_set_p z = preds_of_dfchildX.begin();
00166                      z != preds_of_dfchildX.end();
00167                      z++)
00168                   DF[X][dfchildX].insert(*z);
00169               }
00170             }
00171           }
00172       }
00173 
00174     // ostringstream ost;
00175     // ost << "DF[" << X->dfn() << "] = ";
00176     // output_context oc(cout);
00177     // if (! X)
00178     //  cout << "X is null" << endl;
00179     // else
00180     //  X->output(oc,0);
00181     // for (int j = 0; j < num_nodes; j++)
00182     //  if (DF[X].test(j))
00183     //   ost << j << " ";
00184     // X->comment() = ost.str();
00185 
00186     /*
00187     {
00188       char buf1[2000];
00189       char buf2[300];
00190       
00191 
00192       sprintf(buf1, " %d : DF = { ", X->dfn());
00193       for (basicblock_set_map_p p = DF[X].begin();
00194            p != DF[X].end();
00195            ++p)
00196         {
00197           sprintf(buf2, " %d ", (*p).first->dfn());
00198           strcat(buf1, buf2);
00199         }
00200       strcat(buf1, " } \n");
00201 
00202       sprintf(buf2, " DOM = { ");
00203       strcat(buf1, buf2);
00204       for (basicblock_list_p p = X->children().begin();
00205            p != X->children().end();
00206            ++p)
00207         {
00208           basicblockNode * childX = *p;
00209           sprintf(buf2, " %d ", childX->dfn());
00210           strcat(buf1, buf2);
00211         }
00212       strcat(buf1, " } \n");
00213 
00214       sprintf(buf2, " PRED = { ");
00215       strcat(buf1, buf2);
00216       for (basicblock_list_p p = X->preds().begin();
00217            p != X->preds().end();
00218            ++p)
00219         {
00220           basicblockNode * predX = *p;
00221           sprintf(buf2, " %d ", predX->dfn());
00222           strcat(buf1, buf2);
00223         }
00224       strcat(buf1, " } \n");
00225 
00226       sprintf(buf2, " SUCC = { ");
00227       strcat(buf1, buf2);
00228       for (basicblock_list_p p = X->succs().begin();
00229            p != X->succs().end();
00230            ++p)
00231         {
00232           basicblockNode * succX = *p;
00233           sprintf(buf2, " %d ", succX->dfn());
00234           strcat(buf1, buf2);
00235         }
00236       strcat(buf1, " } \n");
00237 
00238       X->comment() = string(buf1);
00239     }
00240     */
00241   }
00242 
00243 
00244   /* NOT NEEDED
00245   // --- Convert bitset representation into the list-oriented
00246   // representation.
00247 
00248   for (int i = 0; i < df_vec.size(); ++i) {
00249 
00250     // --- Get the current basic block
00251 
00252     basicblockNode * cur = df_vec[i];
00253 
00254     // --- Get the bit-vector representation of its DF
00255 
00256     basicblock_set & DFblock = DF[cur];
00257 
00258     // --- Add an empty list into "this" for the block
00259 
00260     pair<basicblockNode *, basicblock_list> el(cur, basicblock_list());
00261     pair<basicblock_map_p, bool> result = insert(el);
00262 
00263     // --- Get a reference to that list
00264 
00265     basicblock_list & bbl = ( * (result.first)).second;
00266 
00267     // --- Translate each bit into a basicblockNode and insert in the
00268     // list.
00269 
00270     for (int j = 0; j < num_nodes; j++)
00271       if (DFblock.test(j)) {
00272         basicblockNode * Y = df_vec[j];
00273         bbl.push_back(Y);
00274       }
00275   }
00276   */
00277 }

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