OpenADFortTk (basic)
src/lib/support/WhirlParentize.cxx
Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 // $Header: /m_home/m_utkej/Argonne/cvs2svn/cvs/OpenADFortTk/src/lib/support/WhirlParentize.cxx,v 1.4 2007-10-08 18:28:33 utke Exp $
00003 
00004 
00005 #include "Open64IRInterface/Open64BasicTypes.h"
00006 #include "WhirlParentize.h"
00007 
00008 
00009 namespace fortTkSupport { 
00010 
00011   WN* 
00012   FindParentWNBlock(const WN* wn_tree, const WN* wn)
00013   {
00014     if (!wn_tree || !wn) { return NULL; }
00015 
00016     OPERATOR opr = WN_operator(wn_tree);
00017     if (!OPERATOR_is_scf(opr)) {
00018       // 'wn_tree' is not structured control flow and cannot contain blocks
00019       return NULL;
00020     } else {
00021 
00022       WN* blkWN = NULL;
00023       if (opr == OPR_BLOCK) {
00024       
00025         // Test to see if 'wn' is a child of 'wn_tree'
00026         WN *kid = WN_first(wn_tree);
00027         while (kid) {
00028         
00029           // Test this child
00030           if (kid == wn) {
00031             return const_cast<WN*>(wn_tree); // we found the parent block|
00032           } 
00033 
00034           // Recursively test 
00035           if ( (blkWN = FindParentWNBlock(kid, wn)) ) {
00036             return blkWN;
00037           }
00038 
00039           kid = WN_next(kid);
00040         }
00041       } else {
00042       
00043         // Recur on for non-block structured control flow
00044         for (INT kidno = 0; kidno < WN_kid_count(wn_tree); kidno++) {
00045           WN* kid = WN_kid(wn_tree, kidno);
00046           if ( (blkWN = FindParentWNBlock(kid, wn)) ) {
00047             return blkWN;
00048           }
00049         }
00050 
00051       }
00052 
00053       return NULL; // not found
00054     }
00055   }
00056 
00057   //***************************************************************************
00058     // WhirlParentMap
00059     //***************************************************************************
00060 
00061       // Note: whirl2f implementation of parentizing for each PU
00062       // W2F_Push_PU
00063       //   MEM_POOL_Push(&W2F_Parent_Pool);
00064       //   W2CF_Parent_Map = WN_MAP_Create(&W2F_Parent_Pool);
00065       //   W2CF_Parentize(pu);
00066       // W2F_Pop_PU
00067       //   WN_MAP_Delete(W2CF_Parent_Map);
00068       //   W2CF_Parent_Map = WN_MAP_UNDEFINED;
00069       //   MEM_POOL_Pop(&W2F_Parent_Pool);
00070 
00071     WhirlParentMap::WhirlParentMap() 
00072     {
00073       Ctor();
00074     }
00075 
00076   WhirlParentMap::WhirlParentMap(const WN* wn)
00077   {
00078     Ctor();
00079     Create(wn);
00080   }
00081 
00082   void 
00083   WhirlParentMap::Ctor()
00084   {
00085     // Create a pool to hold the parent map for every PU, one at a time.
00086     MEM_POOL_Initialize(&memPool, "WhirlParentMap_Pool", FALSE);
00087     MEM_POOL_Push(&memPool);   
00088     parentMap = WN_MAP_UNDEFINED;
00089   }
00090 
00091   WhirlParentMap::~WhirlParentMap() 
00092   { 
00093     MEM_POOL_Pop(&memPool);
00094     MEM_POOL_Delete(&memPool);
00095   }
00096 
00097   void 
00098   WhirlParentMap::Create(const WN* wn)
00099   {
00100     if (parentMap != WN_MAP_UNDEFINED) {
00101       Clear();
00102     }
00103   
00104     parentMap = WN_MAP_Create(&memPool);
00105     Parentize(wn);
00106   }
00107 
00108   void 
00109   WhirlParentMap::Clear()
00110   {
00111     WN_MAP_Delete(parentMap);
00112     parentMap = WN_MAP_UNDEFINED;
00113   }
00114 
00115   WN* 
00116   WhirlParentMap::FindBlock(const WN* wn)
00117   {
00118     WN* curWN = const_cast<WN*>(wn);
00119     while ( (curWN = Find(curWN)) ) {
00120       if (WN_operator(curWN) == OPR_BLOCK) {
00121         return curWN;
00122       }
00123     }
00124     return NULL;
00125   }
00126 
00127   // Parentize: Given a tree, initialize its parent pointers, overriding
00128   // anything that may have been in the map.  Does not update parent
00129   // pointer of the root node 'wn'.
00130   void 
00131   WhirlParentMap::Parentize(const WN* wn)
00132   {
00133     OPERATOR opr = WN_operator(wn);
00134   
00135     if (!OPERATOR_is_leaf(opr)) {
00136       if (opr == OPR_BLOCK) { // WN_opcode(wn) == OPC_BLOCK
00137         WN *kid = WN_first(wn);
00138         while (kid) {
00139           Insert(kid, wn);
00140           Parentize(kid);
00141           kid = WN_next(kid);
00142         }
00143       } else {
00144         for (INT kidno = 0; kidno < WN_kid_count(wn); kidno++) {
00145           WN* kid = WN_kid(wn, kidno);
00146           if (kid) { 
00147             Insert(kid, wn);
00148             Parentize(kid);
00149           }
00150         }
00151       }
00152     }
00153   }
00154 
00155 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines