Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
wn_util.h
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 /*---------------------------------------------------------------------
00037 ***                  WN walk, insert and copy routines        
00038 ***
00039 *** Description:
00040 ***  
00041 ***  This file contains routines that operate on a given WHIRL tree
00042 ***  The operations performed on the tree include walking the tree
00043 ***  inserting a node into a block of nodes and copying a tree 
00044 ***
00045 ***
00046 *** Reserved prefix:
00047 *** WN_WALK    for WHIRL node walk routines
00048 *** WN_INSERT  for WHIRL node insert routines
00049 *** WN_COPY    for WHIRL node copy routines
00050 *** WN_DELETE  for WHIRL node delete routines
00051 *** WN_EXTRACT for WHIRL node extraction (but not delete) routines
00052 *** WN_LOOP    for WHIRL node loop routines
00053 ***
00054 ***
00055 ***
00056 *** Exported types:
00057 ***
00058 ***
00059 ***  WN_ITER
00060 ***
00061 *** A tree node iterator. This type has one client visible field
00062 ***
00063 ***     WN *wn
00064 ***   
00065 ***       Gives the current tree node.
00066 ***
00067 ***
00068 ***
00069 *** Exported functions:
00070 ***
00071 ***
00072 ***   WN_ITER* WN_WALK_TreeIter(
00073 ***      WN* wn)
00074 ***   
00075 ***
00076 ***   Creates and returns a tree iterator. The tree iterator iterates over
00077 ***   all nodes in the tree including structured control flow nodes,
00078 ***   statement nodes, and expression nodes
00079 *** 
00080 ***
00081 ***   WN_ITER* WN_WALK_TreeNext(
00082 ***      WN_ITER *wni)
00083 *** 
00084 ***
00085 ***   Walks the tree in preorder and returns the tree iterator 
00086 ***   containing the next node in the tree walk
00087 ***   
00088 ***
00089 ***
00090 ***   WN_ITER *WN_WALK_SCFIter(
00091 ***      WN_ITER *wni)
00092 ***
00093 ***   
00094 ***   Creates and returns a tree iterator. The tree iterator iterates over 
00095 ***   nodes at the structured control flow level
00096 ***
00097 *** 
00098 ***   WN_ITER *WN_WALK_SCFNext(
00099 ***      WN_ITER *wni)
00100 ***
00101 ***   Walks the tree in preorder and returns the next node whose
00102 ***   opcode falls into the structured control flow category
00103 ***
00104 ***
00105 ***
00106 ***   WN_ITER *WN_WALK_StmtIter(
00107 ***      WN *wn)
00108 ***
00109 ***   Creates and returns a tree iterator. The tree iterator will return
00110 ***   all statement nodes and structured control flow nodes. For expressions
00111 ***   it will only return them if they are kids of structured control flow
00112 ***   nodes. 
00113 ***
00114 ***
00115 ***
00116 ***  WN_ITER *WN_WALK_StmtNext(
00117 ***     WN_ITER *wni)
00118 ***
00119 ***  Walks the tree in preorder and returns the next node, the node
00120 ***  must fall into the category described in WN_StmtIter
00121 ***
00122 ***
00123 ***
00124 ***  WN_ITER *WN_WALK_Abort(
00125 ***     WN_ITER *wni)
00126 ***
00127 ***  To exit prematurely from a tree walk a call to this routine must
00128 ***  be made. It frees the data structures used during a walk operation
00129 ***
00130 ***   
00131 ***
00132 ***  void WN_INSERT_BlockBefore(
00133 ***     WN* b, 
00134 ***     WN* wn,
00135 ***     WN* in)
00136 ***
00137 ***  To insert a node before a given node in a block node. Node "in" is
00138 ***  inserted before node "wn" in block "b". Note, wn may be NULL, in that
00139 ***  case "in" is inserted at the end. Also, "in" may be a block,
00140 ***  in which case all the statements in the block are inserted
00141 ***
00142 ***  void WN_INSERT_BlockFirst(
00143 ***     WN* b,
00144 ***     WN* in)
00145 ***
00146 ***  To insert a node before the first node in a block.  Note, "in" may be
00147 ***  a block, in which case all the statements in the block are inserted.
00148 ***
00149 ***
00150 ***  void WN_INSERT_BlockAfter(
00151 ***     WN* b, 
00152 ***     WN* wn,
00153 ***     WN* in)
00154 ***
00155 ***  To insert a node after a given node in a block node. Node "in" is
00156 ***  inserted after node "wn" in block "b".  Note, "wn" may be NULL, in that
00157 ***  case "in" is inserted at the beginning. Also, "in" may be a block,
00158 ***  in which case all the statements in the block are inserted
00159 ***
00160 ***  void WN_INSERT_BlockLast(
00161 ***     WN* b,
00162 ***     WN* in)
00163 ***
00164 ***  To insert a node after the last node in a block.  Note, "in" may be
00165 ***  a block, in which case all the statements in the block are inserted.
00166 ***
00167 ***
00168 ***
00169 ***  WN* WN_COPY_Tree(
00170 ***    WN* wn)
00171 ***
00172 ***  To copy node wn.
00173 ***  No annotations are copied at this point. This is strictly a tree copy,
00174 ***  and a recursive one
00175 ***
00176 ***
00177 ***  WN* WN_COPY_Tree_With_Map(
00178 ***    WN* wn)
00179 ***
00180 ***  To copy node wn and its map.
00181 ***  This is strictly a tree copy, and a recursive one
00182 ***
00183 ***
00184 ***  void WN_COPY_All_Maps(
00185 ***    WN* dst,
00186 ***    WN* src)
00187 ***
00188 ***  To copy all maps from src to dst.
00189 ***
00190 ***
00191 ***  void WN_DELETE_Tree(
00192 ***     WN *tree)
00193 ***
00194 ***  Deletes a tree recursively. No annotations are deleted at this point. 
00195 ***  
00196 ***
00197 ***  WN* WN_DELETE_FromBlock(
00198 ***       WN* blck, 
00199 ***       WN* wn)
00200 ***
00201 ***  To delete node "wn" from a BLOCK node. Note, WN_DELETE_Tree is called 
00202 ***  which is a recursive delete, it deletes all the kids.
00203 *** 
00204 ***  WN *WN_EXTRACT_FromBlock(
00205 ***       WN *parent,
00206 ***       WN *item)
00207 ***
00208 ***  Extract node "item" from a BLOCK, but don't delete it.
00209 ***
00210 ***  WN *WN_EXTRACT_ItemsFromBlock(
00211 ***       WN *parent,
00212 ***       WN *first_item,
00213 ***       WN *last_item)
00214 ***  Extract nodes between first_item and last_item from a BLOCK,
00215 ***  but don't delete them.
00216 ***
00217 ***  WN *WN_LOOP_InductionVariable(
00218 ***       const WN *loop)
00219 ***
00220 *** Determine the loop's induction variable.  Returns either an
00221 *** IDNAME or LDID.  If unable to determine it, returns NULL.
00222 ***
00223 ***  WN *WN_LOOP_LowerBound(
00224 ***       const WN *loop)
00225 ***
00226 *** Determine the lower bound of the loop, which is basically
00227 *** the value stored to the induction variable to initialize
00228 *** the loop.  If unable to determine it, returns NULL.
00229 ***
00230 ***  WN *WN_LOOP_UpperBound(
00231 ***       const WN *loop,
00232 ***       OPCODE *compare)
00233 ***
00234 *** Determine the upper bound of the loop, which is basically
00235 *** the value which can not be exceeded (going in either
00236 *** direction (positive dir if increment is positive, or
00237 *** negative direction is increment is negative
00238 *** Also returns the comparison operator to know how the iv is
00239 *** related to this upper bound.  We canonicalize it so the iv
00240 *** is always on the lhs of the comparison, and the upper bound
00241 *** is always on the rhs:  iv <,<=,==,!=,>=,> upper bound
00242 *** If unable to determine it, returns NULL.
00243 ***
00244 ***  WN *WN_LOOP_Increment(
00245 ***       const WN *loop,
00246 ***       BOOL *is_incr)
00247 ***
00248 *** Determine the increment (if is_incr = TRUE) value
00249 *** the value which can not be exceeded (going in either
00250 *** direction (positive dir if is_incr is true, or negative
00251 *** direction if is_incr is false (actually decrements iv)
00252 *** If unable to determine it, returns NULL.
00253 ***
00254 ***  WN *WN_LOOP_TripCount(
00255 ***       const WN *loop)
00256 ***
00257 ***  Determine the trip count, which may be a LDID, or INTCONST
00258 ***  If unable to determine it, returns NULL.
00259 ***
00260 ***  WN_object_ty(const WN *) 
00261 ***    Obtain the high level type of the object being accessed.
00262 ***
00263 ***  WN_object_size(const WN *)
00264 ***    Obtain the size of the object being accessed.
00265 ***
00266 ***
00267 ***
00268 ***
00269 ***
00270 ***  void Add_Pragma_To_MP_Regions (WN_VECTOR *wnv, 
00271 ***                                 WN_PRAGMA_ID pragma_id,
00272 ***                                 ST *st, WN_OFFSET ofst,
00273 ***                                 WN_MAP parent_map,
00274 ***                                 BOOL make_compiler_generated)
00275 ***     Given a vector of WHIRL-MP regions innermost to outermost
00276 ***     (i.e. the innermost MP region is first, outermost is last)
00277 ***     add a pragma of the given type for the given ST *as required* 
00278 ***     to the MP regions. The algorithm for adding pragmas 
00279 ***     currently works for LOCAL and SHARED pragmas only.
00280 ***     Also, if the parent_map is non-NULL, then update the
00281 ***     parent-map when inserting.
00282 ***
00283 ***
00284 ***------------------------------------------------------------------*/
00285 
00286 
00287 #ifndef wn_util_INCLUDED
00288 #define wn_util_INCLUDED
00289 
00290 #include "wn.h"
00291 
00292 #ifdef __cplusplus
00293 extern "C" {
00294 #endif
00295 
00296 typedef struct wn_stack{
00297  WN** stack;
00298  WN** sp;
00299  INT size;
00300 } WN_STACK;
00301 
00302 typedef struct wn_iter {
00303   WN *wn;               /* current tree node */
00304   WN_STACK *stack;      /* stack used during the walk */
00305 } WN_ITER;
00306 
00307 /* access macro */
00308 
00309 #define WN_STACK_stack(wns) ((wns)->stack)
00310 #define WN_STACK_sp(wns) ((wns)->sp)
00311 #define WN_STACK_size(wns) ((wns)->size)
00312 
00313 #define WN_ITER_wn(wni) ((wni)->wn)
00314 #define WN_ITER_stack(wni) ((wni)->stack)
00315 
00316 extern WN_ITER* WN_WALK_TreeIter(
00317        WN*
00318 );
00319 
00320 extern WN_ITER* WN_WALK_TreeNext(
00321        WN_ITER*
00322 );
00323 
00324 extern WN_ITER* WN_WALK_StmtIter(
00325        WN*
00326 );
00327 
00328 extern WN_ITER* WN_WALK_StmtNext(
00329        WN_ITER*
00330 );
00331 
00332 extern WN_ITER* WN_WALK_SCFIter(
00333        WN *wn
00334 );
00335 
00336 extern WN_ITER* WN_WALK_SCFNext(
00337        WN_ITER*
00338 );
00339 
00340 extern void WN_WALK_Abort(
00341        WN_ITER*
00342 );
00343 
00344 extern void WN_INSERT_BlockBefore(
00345        WN* b, 
00346        WN* wn,
00347        WN* in
00348 );
00349 
00350 #define WN_INSERT_BlockFirst(b, in) \
00351     WN_INSERT_BlockBefore(b, WN_first(b), in)
00352 
00353 extern void WN_INSERT_BlockAfter(
00354        WN* b, 
00355        WN* wn, 
00356        WN* in
00357 );
00358 
00359 #define WN_INSERT_BlockLast(b, in) \
00360     WN_INSERT_BlockAfter(b, WN_last(b), in)
00361 
00362 extern WN* WN_COPY_Tree(
00363        WN *tree_node
00364 );
00365 
00366 extern WN* WN_COPY_Tree_With_Map(
00367        WN *tree_node
00368 );
00369 
00370 extern void WN_COPY_All_Maps(
00371        WN *dst,
00372        WN *src
00373 );
00374 
00375 extern void WN_DELETE_Tree(
00376        WN* tree
00377 );
00378 
00379 extern void WN_DELETE_FromBlock(
00380        WN *blck,
00381        WN *wn
00382 );
00383 
00384 WN *WN_EXTRACT_FromBlock(
00385        WN *parent,
00386        WN *item
00387 );
00388 
00389 WN *WN_EXTRACT_ItemsFromBlock(
00390         WN *parent, 
00391         WN *first_item, 
00392         WN *last_item
00393 );
00394 
00395 WN *WN_LOOP_LowerBound(
00396         const WN *loop
00397 );
00398 
00399 WN *WN_LOOP_UpperBound(
00400         const WN *loop,
00401         OPCODE *compare
00402 );
00403 
00404 WN *WN_LOOP_Increment(
00405         const WN *loop,
00406         BOOL *is_incr
00407 );
00408 
00409 WN *WN_LOOP_TripCount(
00410         const WN *loop
00411 );
00412 
00413 WN *WN_LOOP_InductionVariable(
00414         const WN *loop
00415 );
00416 
00417 
00418 
00419 /*  Other WN utilities */
00420 
00421 /* Obtain the high-level type of the item accessed */
00422 extern TY_IDX WN_object_ty(const WN *);
00423 
00424 /* Obtain the size of object being accessed */
00425 extern INT32 WN_object_size(const WN *);
00426 
00427 inline BOOL WN_is_black_box(const WN *wn) 
00428 { 
00429   return OPCODE_is_black_box( WN_opcode(wn) ); 
00430 }
00431 
00432 #ifdef __cplusplus
00433 } /* close extern "C" */
00434 
00435 
00436 /* Needed for the STL vector class used below
00437  */
00438 #include "vector"
00439 #include "mempool_allocator.h"
00440 
00441 typedef mempool_allocator<WN*> VEC_POOL_ALLOCATOR;
00442 typedef std::vector<WN*, VEC_POOL_ALLOCATOR> WN_VECTOR;
00443 
00444 extern "C" void Add_Pragma_To_MP_Regions (WN_VECTOR *wnv, 
00445                                           WN_PRAGMA_ID pragma_id,
00446                                           ST *st, WN_OFFSET ofst,
00447                                           WN_MAP parent_map,
00448                                           BOOL make_compiler_generated);
00449 
00450 #endif /* __cplusplus */
00451 
00452 #endif /* wn_util_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines