Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
ir_graph.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  * ====================================================================
00038  *
00039  * Author: Seema Hiranandani
00040  *
00041  *
00042  * Revision history:
00043  *  19-Aug-95 - Original Version
00044  *
00045  * Description:
00046  *
00047  * This module contains data structures for a graph abstraction.
00048  * This graph is used to construct, for instance, a call graph.
00049  *
00050  * ====================================================================
00051  * ====================================================================
00052  */
00053 
00054 #ifndef ip_graph_INCLUDED
00055 #define ip_graph_INCLUDED
00056 #ifdef __cplusplus
00057 extern "C" {
00058 #endif
00059 
00060 /* ====================================================================
00061  *
00062  * VERTEX / EDGE / GRAPH : Directed graph implementation
00063  *
00064  * A GRAPH is a set of VERTEX objects, connected by a set of EDGE
00065  * objects.  These sets are implemented as a vector of VERTEXes and a
00066  * vector of EDGEs, with all references being indices into these
00067  * vectors.  The sets are allowed to grow (by adding VERTEXes or EDGEs)
00068  * or to shrink (by deleting them).  Growth may cause reallocation of
00069  * the vector.  To minimize reallocation cost, it is done in larger
00070  * chunks than necessary.  To avoid rearrangement of the data upon
00071  * deletion, the deleted VERTEX/EDGE is simply marked invalid.
00072  *
00073  * ====================================================================
00074  */
00075 
00076 /* EDGE/VERTEX index types, and reserved index values: */
00077 typedef int EINDEX;
00078 typedef int VINDEX;
00079 #define INVALID_EINDEX -1
00080 #define INVALID_VINDEX -1
00081 
00082 /* ====================================================================
00083  *
00084  * A VERTEX contains two singly-linked lists of EDGEs, one of EDGEs
00085  * starting at that VERTEX, and one of EDGEs ending at that VERTEX.  
00086  * Each list is represented in the VERTEX by the index of its first
00087  * element (VERTEX_from and VERTEX_to) and a count of its elements
00088  * (VERTEX_fcnt and VERTEX_tcnt).  An invalid (i.e. unused) VERTEX is
00089  * indicated by VERTEX_fcnt == INVALID_EINDEX.  The VERTEX also
00090  * contains a pointer VERTEX_user to additional data required by the
00091  * client derived graph. The level of a vertex is defined as the (max level
00092  * of the immediate successors of the vertex) + 1
00093  * ====================================================================
00094  */
00095 
00096 typedef struct vertex {
00097   void* user;         /* user information */
00098   EINDEX from, to;    /* first edge from and to this vertex */
00099   EINDEX fcnt, tcnt;  /* from/to counts                     */
00100   int level;          /* level of the vertex                */
00101 } VERTEX;
00102 
00103 /* Field access macros: */
00104 #define VERTEX_user(vertex) ((vertex)->user)
00105 #define VERTEX_from(vertex) ((vertex)->from)
00106 #define VERTEX_to(vertex)   ((vertex)->to)
00107 #define VERTEX_fcnt(vertex) ((vertex)->fcnt)
00108 #define VERTEX_tcnt(vertex) ((vertex)->tcnt)
00109 #define VERTEX_level(vertex) ((vertex)->level)
00110 /* ====================================================================
00111  *
00112  * A EDGE contains two VERTEXes, its from VERTEX (EDGE_from) and its
00113  * to vertex (EDGE_to).  It contains links (indices) to the next edges
00114  * in the VERTEX_from list for its from VERTEX (EDGE_nfrom), and in the
00115  * VERTEX_to list for its to VERTEX (EDGE_nto).  It contains a flag
00116  * word containing attributes, and a pointer EDGE_user to additional
00117  * data required by the client derived graph.
00118  *
00119  * ====================================================================
00120  */
00121 
00122 /* EDGE data structure: */
00123 
00124 typedef int ETYPEX;
00125 typedef int BOOL;
00126 #ifndef FALSE
00127 #define FALSE 0
00128 #define TRUE  1
00129 #endif
00130 typedef char MEM_POOL;
00131 
00132 typedef struct edge {
00133   void * user;        /* user information     */
00134   VINDEX from, to;    /* from and to vertices */
00135   EINDEX nfrom;       /* next edge from the from vertex */
00136   EINDEX nto;         /* next edge to the to vertex */
00137   ETYPEX etype;       /* edge type, i.e. is it a back edge resulting */
00138                       /* in a cycle: used to locate recursion  */
00139 } EDGE; 
00140 
00141 /* Field access macros: */
00142 #define EDGE_user(edge)  ((edge)->user)
00143 #define EDGE_from(edge)  ((edge)->from)
00144 #define EDGE_to(edge)    ((edge)->to)
00145 #define EDGE_nfrom(edge) ((edge)->nfrom)
00146 #define EDGE_nto(edge)   ((edge)->nto)
00147 #define EDGE_etype(edge)   ((edge)->etype)
00148 
00149 /* Flag access: */
00150 #define EDGE_RECURSIVE 1
00151 #define Set_EDGE_recursive(edge) (EDGE_etype(edge) |= EDGE_RECURSIVE)
00152 #define EDGE_recursive(edge)   (EDGE_etype(edge) & EDGE_RECURSIVE)
00153 
00154 /* ====================================================================
00155  *
00156  * A GRAPH is the top-level directed graph data structure.  For both
00157  * vertices and edges, it contains the number of VERTEX/EDGE structures
00158  * actually allocated (GRAPH_vmax, GRAPH_emax), the number of actual
00159  * VERTEX/EDGE components of the graph (GRAPH_vcnt, GRAPH_ecnt), the
00160  * number of free VERTEX/EDGE structures that are allocated and
00161  * available for use (GRAPH_vfree, GRAPH_efree), and the vector of
00162  * VERTEX_EDGE components (GRAPH_v, GRAPH_e).  It also contains the
00163  * root of the graph (GRAPH_root) and the memory pool to use for
00164  * allocation (GRAPH_m).
00165  *
00166  * ====================================================================
00167  */
00168 
00169 typedef struct graph {
00170   VINDEX vcnt;  /* vertex count            */
00171   VINDEX vmax;  /* max vertices            */
00172   VINDEX vfree; /* number of free vertices */
00173 
00174   EINDEX ecnt;  /* edge count              */
00175   EINDEX emax;  /* max edges               */
00176   EINDEX efree; /* number of free edges    */
00177  
00178   VINDEX root;  /* root of the graph       */
00179   VERTEX *v;    /* list of vertices        */
00180   EDGE *e;      /* list of edges           */
00181   MEM_POOL *m;  /* mem pool                */
00182 } GRAPH;
00183 
00184 /* Field access macros: */
00185 #define GRAPH_vcnt(g) ((g)->vcnt)
00186 #define GRAPH_vmax(g) ((g)->vmax)
00187 #define GRAPH_vfree(g)((g)->vfree)
00188 #define GRAPH_ecnt(g) ((g)->ecnt)
00189 #define GRAPH_emax(g) ((g)->emax)
00190 #define GRAPH_efree(g) ((g)->efree)
00191 #define GRAPH_root(g)  ((g)->root)
00192 #define GRAPH_m(g) ((g)->m)
00193 #define GRAPH_v(g)  ((g)->v)
00194 #define GRAPH_v_i(g,index) ((g)->v[index])
00195 #define GRAPH_e(g)  ((g)->e)
00196 #define GRAPH_e_i(g,index) ((g)->e[index])
00197 
00198 /* Walk all the valid vertices in the graph g, using VINDEX v: */
00199 #define FOR_EACH_VERTEX(g,v)    \
00200         for ( v = 0; v < GRAPH_vmax(g); v++ )   \
00201           if ( VERTEX_fcnt (&GRAPH_v_i(g,v)) != INVALID_VINDEX )
00202 
00203 /* ====================================================================
00204  *
00205  * DFN: Depth-first numbering graph traversal control struct
00206  *
00207  * This struct is used to store an ordering of the nodes in a graph, to
00208  * control traversal in a particular order.  It contains a vector of
00209  * the graph vertices in the traversal order (DFN_v_list), and the
00210  * indices of the first useful index in the vector (GRAV_first) and of
00211  * the index after the last useful index in the vector (DFN_end).
00212  *
00213  * ====================================================================
00214  */
00215 
00216 typedef struct dfn {
00217   int first;
00218   int end;
00219   VINDEX* v_list;
00220 } DFN;
00221 
00222 /* Field access macros: */
00223 #define DFN_v_list(d) ((d)->v_list)
00224 #define DFN_v_list_i(d,i) ((d)->v_list[i])
00225 #define DFN_first(d) ((d)->first)
00226 #define DFN_end(d) ((d)->end)
00227 
00228 /* Trace a DFN ordering: */
00229 extern void Print_DFN ( DFN * );
00230 
00231 /* Construct a depth-first numbering of the given graph: */
00232 extern DFN* Depth_First_Ordering ( GRAPH *, MEM_POOL * );
00233 
00234 /* De-allocate a DFN ordering: */
00235 extern void Free_DFN ( DFN *, MEM_POOL * );
00236 
00237 /* ====================================================================
00238  *
00239  * V_ITER: Vertex iterator control struct
00240  *
00241  * This struct is used to control an iterator over vertices.
00242  *
00243  * ====================================================================
00244  */
00245 
00246 typedef struct v_iter {
00247   GRAPH *g;       /* the graph */
00248   VINDEX c_v;     /* the current vertex */
00249   EINDEX from_e;  /* from edge   */
00250   EINDEX to_e;    /* to edge     */
00251   EINDEX  fcnt;   /* from count  */
00252   EINDEX nfrom;   /* next from edge */
00253   EINDEX tcnt;    /* to count      */
00254   EINDEX nto;     /* next to edges */
00255   EINDEX c_e;     /* current edge  */
00256   MEM_POOL *m;     /* mempool  */
00257 } V_ITER;
00258 
00259 /* Field access macros: */
00260 #define V_ITER_g(vi) ((vi)->g)
00261 #define V_ITER_c_v(vi) ((vi)->c_v)
00262 #define V_ITER_from_e(vi) ((vi)->from_e)
00263 #define V_ITER_to_e(vi) ((vi)->to_e)
00264 #define V_ITER_fcnt(vi) ((vi)->fcnt)
00265 #define V_ITER_nfrom(vi) ((vi)->nfrom)
00266 #define V_ITER_tcnt(vi)  ((vi)->tcnt)
00267 #define V_ITER_nto(vi)   ((vi)->nto)
00268 #define V_ITER_c_e(vi)   ((vi)->c_e)
00269 #define V_ITER_m(vi)   ((vi)->m)
00270 
00271 #define MEM_POOL_Alloc(m, s)            calloc(m, s)
00272 #define MEM_POOL_Realloc(m, ob, os, ns) realloc(m, ns)
00273 #define MEM_POOL_FREE(m, s)             /* we don't free for now */
00274 
00275 /* ====================================================================
00276  *
00277  * External function declarations
00278  *
00279  * ====================================================================
00280  */
00281 
00282 extern GRAPH* build_graph_u(VINDEX, EINDEX, MEM_POOL*);
00283 extern GRAPH* build_graph(MEM_POOL*);
00284 
00285 extern VINDEX add_vertex(GRAPH*, void*);
00286 extern EINDEX add_edge(GRAPH*, VINDEX, VINDEX, void*);
00287 
00288 extern BOOL is_vertex(GRAPH*, VINDEX);
00289 extern BOOL is_edge(GRAPH*, EINDEX);
00290 
00291 extern void  delete_edge(GRAPH*, EINDEX);
00292 extern void* delete_vertex(GRAPH*, VINDEX);
00293 
00294 extern void* get_vertex(GRAPH*, VINDEX);
00295 extern void* get_edge(GRAPH*, VINDEX, VINDEX);
00296 extern void* get_edge_u(GRAPH*, EINDEX );
00297 extern void set_edge_u (GRAPH *, EINDEX, void *);
00298 
00299 extern int num_preds(GRAPH*, VINDEX);
00300 extern int num_succs(GRAPH*, VINDEX);
00301 extern int edge_count(GRAPH*, VINDEX from, VINDEX to);
00302 
00303 extern VINDEX next_vertex(GRAPH *g, VINDEX vertex);
00304 
00305 extern V_ITER* create_vertex_iter(GRAPH*, VINDEX, MEM_POOL*);
00306 extern VINDEX  first_v_preds(V_ITER*);
00307 extern VINDEX  next_v_preds(V_ITER*);
00308 extern VINDEX  first_v_succs(V_ITER*);
00309 extern VINDEX  next_v_succs(V_ITER*);
00310 
00311 /* related to the level of a vertex in the graph */
00312 extern void set_vertex_level(GRAPH*, VINDEX r, int level);
00313 extern int get_vertex_level(GRAPH*, VINDEX r);
00314 
00315 #ifdef __cplusplus
00316 }
00317 #endif
00318 #endif /* ip_graph_INCLUDED */
00319 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines