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  

dead.cc

Go to the documentation of this file.
00001 // $Id: dead.cc,v 1.5 2003/08/07 23:14:12 pnav Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 //
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 //
00009 //  Lipphei Adam
00010 //  Samuel Z. Guyer
00011 //  Daniel A. Jimenez
00012 //  Calvin Lin
00013 //
00014 //  Permission is hereby granted, free of charge, to any person
00015 //  obtaining a copy of this software and associated documentation
00016 //  files (the "Software"), to deal in the Software without
00017 //  restriction, including without limitation the rights to use, copy,
00018 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00019 //  of the Software, and to permit persons to whom the Software is
00020 //  furnished to do so, subject to the following conditions:
00021 //
00022 //  The above copyright notice and this permission notice shall be
00023 //  included in all copies or substantial portions of the Software.
00024 //
00025 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00026 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00027 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00028 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00029 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00030 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00031 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00032 //  THE SOFTWARE.
00033 //
00034 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00035 //  Computer Science for inspiring parts of the C-Breeze design.
00036 //
00037 // ----------------------------------------------------------------------
00038 //
00039 // dead.cc
00040 //
00041 // Dead code elimination
00042 //
00043 
00044 #include "c_breeze.h"
00045 #include "live.h"
00046 #include "dead.h"
00047 
00048 typedef stmt_list::reverse_iterator stmt_list_rp;
00049 
00050 deadCodeEliminationChanger::deadCodeEliminationChanger () :
00051   Changer(Both, Subtree, false), _lw(NULL)
00052 {}
00053 
00054 Node * deadCodeEliminationChanger::at_proc (procNode * proc, Order ord) {
00055   if (ord == Preorder) {
00056     // first perform the liveness analysis on the procedure
00057     // to get def, use, live_in and live_out information
00058     _lw = LivenessWalker::walk(proc);
00059   }
00060   else {  // ord == Postorder
00061     UnusedDeclarationCleanupChanger udc;
00062     proc->change(udc);  // remove any unused declarations
00063 
00064     if (_lw) delete _lw;
00065   }
00066   return proc;
00067 }
00068 
00069 Node * deadCodeEliminationChanger::at_threeAddr (threeAddrNode * node, Order ord) {
00070   if (ord == Postorder) {
00071     if (node->lhs()) {  // this is an assignment statement
00072       id_list& ids = node->lhs()->fields();      
00073       // Assumption: non-empty fields = struct, non-empty index = array
00074       if ( (! ids.empty()) || node->lhs()->index() ) {
00075         return node;  // skip any dead-code elimination
00076       }
00077 
00078       // get the live_out for this stmt
00079       decl_set * live_out = _lw->live_out(node);
00080       // get the defs for this stmt
00081       decl_set * defs = _lw->defs(node);
00082 
00083       bool in_live_out = false;
00084       for (decl_set_p iter = defs->begin() ; iter != defs->end() ; iter++) {
00085         declNode * decl = (* iter);
00086         // if this definition is in the live_out set,
00087         if (live_out->find(decl) != live_out->end()) {
00088           in_live_out = true;
00089           break;
00090         }
00091       }
00092 
00093       // if this definition is NOT in the live_out set,
00094       // then it is a dead code
00095       if (! in_live_out) {
00096 //        cout << "Deleting var " << decl->name() << endl;
00097         return NULL;
00098       }
00099 //      else cout << "Var " << decl->name() << " is not deleted." << endl;
00100     }
00101   }
00102   return node;
00103 }

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