dumper and datastructure for callgraph
[libfirm] / ir / ir / irnode_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irnode_t.h
4  * Purpose:     Representation of an intermediate operation -- private header.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13
14 /**
15  * @file irnode_t.h
16  *
17  * Declarations of an ir node.
18  *
19  * @author Martin Trapp, Christian Schaefer
20  */
21
22
23 # ifndef _IRNODE_T_H_
24 # define _IRNODE_T_H_
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 # include "irnode.h"
31 # include "irop_t.h"
32 # include "irgraph_t.h"
33 # include "irflag_t.h"
34 # include "firm_common_t.h"
35 # include "irdom_t.h" /* For size of struct dom_info. */
36 # include "dbginfo.h"
37 # include "irloop.h"
38 # include "array.h"
39
40 # include "exc.h"
41
42 #include "set.h"
43 #include "entity_t.h"
44 #include "type_t.h"
45
46 /** ir node attributes **/
47
48 /** Block attributes */
49 typedef struct {
50   /* General attributes */
51   ir_graph *irg;
52   unsigned long block_visited;  /**< for the walker that walks over all blocks. */
53   /* Attributes private to construction: */
54   bool matured;               /**< if set, all in-nodes of the block are fixed */
55   struct ir_node **graph_arr; /**< array to store all parameters */
56   /* Attributes holding analyses information */
57   struct dom_info dom;        /**< Datastructure that holds information about dominators.
58                  @@@ @todo
59                  Eventually overlay with graph_arr as only valid
60                  in different phases.  Eventually inline the whole
61                  datastructure. */
62   /*   exc_t exc;  */            /**< role of this block for exception handling */
63   /*   ir_node *handler_entry; */    /**< handler entry block iff this block is part of a region */
64   ir_node ** in_cg;           /**< array with predecessors in
65                    * interprocedural_view, if they differ
66                    * from intraprocedural predecessors */
67   int *backedge;              /**< Field n set to true if pred n is backedge.
68                      @@@ @todo Ev. replace by bitfield! */
69   int *cg_backedge;           /**< Field n set to true if pred n is interprocedural backedge.
70                      @@@ @todo Ev. replace by bitfield! */
71 } block_attr;
72
73 /** Start attributes */
74 typedef struct {
75   char dummy;
76   /*   ir_graph *irg;   @@@ now in block */
77 } start_attr;
78
79 /** Cond attributes */
80 typedef struct {
81   cond_kind kind;    /**< flavor of Cond */
82   long default_proj; /**< for optimization: biggest Proj number, i.e. the one
83               used for default. */
84 } cond_attr;
85
86 /** Const attributes */
87 typedef struct {
88   tarval *tv;        /**< the target value */
89   type   *tp;        /**< the source type, for analyses. default: type_unknown. */
90 } const_attr;
91
92 typedef struct {
93   symconst_symbol sym;  // old tori
94   symconst_kind num;
95   type *tp;          /**< the source type, for analyses. default: type_unknown. */
96 } symconst_attr;
97
98 /** Sel attributes */
99 typedef struct {
100   entity *ent;          /**< entity to select */
101 } sel_attr;
102
103 typedef struct {
104   type *cld_tp;         /**< type of called procedure */
105 #if PRECISE_EXC_CONTEXT
106   struct ir_node **frag_arr; /**< For Phi node construction in case of exceptions */
107 #endif
108   entity ** callee_arr; /**< result of callee analysis */
109 } call_attr;
110
111 /** Alloc attributes */
112 typedef struct {
113   type *type;           /**< Type of the allocated object.  */
114   where_alloc where;    /**< stack, heap or other managed part of memory */
115 #if PRECISE_EXC_CONTEXT
116   struct ir_node **frag_arr; /**< For Phi node construction in case of exceptions */
117 #endif
118 } alloc_attr;
119
120 /** InstOf attributes */
121 typedef struct
122 {
123   type *ent;
124   int dfn;
125 } io_attr;
126
127 /** Filter attributes */
128 typedef struct {
129   long proj;                 /**< contains the result position to project (Proj) */
130   ir_node ** in_cg;          /**< array with interprocedural predecessors (Phi) */
131   int *backedge;              /**< Field n set to true if pred n is backedge.
132                      @todo Ev. replace by bitfield! */
133 } filter_attr;
134
135 /** EndReg/EndExcept attributes */
136 typedef struct {
137   char dummy;
138   /*   ir_graph * irg; */           /**< ir_graph this node belongs to (for */
139   /*                               * navigating in interprocedural graphs)  */
140   /*                       @@@ now in block */
141 } end_attr;
142
143 /** CallBegin attributes */
144 typedef struct {
145   /*   ir_graph * irg; */           /**< ir_graph this node belongs to (for */
146   /*                   * navigating in interprocedural graphs) */
147   /*                            @@@ now in block */
148   ir_node * call;            /**< associated Call-operation */
149 } callbegin_attr;
150
151 /** Cast attributes */
152 typedef struct {
153   type *totype;
154 } cast_attr;
155
156 typedef pn_Cmp confirm_attr; /** Attribute to hold compare operation */
157
158 /** Some irnodes just have one attribute, these are stored here,
159    some have more. Their name is 'irnodename_attr' */
160 typedef union {
161   start_attr     start; /**< For Start */
162   block_attr     block; /**< For Block: Fields needed to construct it */
163   cond_attr      c;     /**< For Cond. */
164   const_attr     con;   /**< For Const: contains the value of the constant and a type */
165   symconst_attr  i;     /**< For SymConst. */
166   sel_attr       s;     /**< For Sel. */
167   call_attr      call;  /**< For Call: pointer to the type of the method to call */
168   callbegin_attr callbegin; /**< For CallBegin */
169   alloc_attr     a;     /**< For Alloc. */
170   io_attr        io;    /**< For InstOf */
171   type          *f;     /**< For Free. */
172   cast_attr      cast;  /**< For Cast. */
173   int            phi0_pos;  /**< For Phi. Used to remember the value defined by
174                    this Phi node.  Needed when the Phi is completed
175                    to call get_r_internal_value to find the
176                    predecessors. If this attribute is set, the Phi
177                    node takes the role of the obsolete Phi0 node,
178                    therefore the name. */
179   int *phi_backedge;    /**< For Phi after construction.
180                Field n set to true if pred n is backedge.
181                @todo Ev. replace by bitfield! */
182   long           proj;  /**< For Proj: contains the result position to project */
183   confirm_attr   confirm_cmp;   /**< For Confirm: compare operation */
184   filter_attr    filter;    /**< For Filter */
185   end_attr       end;       /**< For EndReg, EndExcept */
186 #if PRECISE_EXC_CONTEXT
187   struct ir_node **frag_arr; /**< For Phi node construction in case of exceptions
188                 for nodes Store, Load, Div, Mod, Quot, DivMod. */
189 #endif
190 } attr;
191
192
193 /** common structure of an irnode
194     if the node has some attributes, they are stored in attr */
195 struct ir_node {
196   /* ------- Basics of the representation  ------- */
197   firm_kind kind;          /**< distinguishes this node from others */
198   ir_op *op;               /**< Opcode of this node. */
199   ir_mode *mode;           /**< Mode of this node. */
200   unsigned long visited;   /**< visited counter for walks of the graph */
201   struct ir_node **in;     /**< array with predecessors / operands */
202   void *link;              /**< to attach additional information to the node, e.g.
203                               used while construction to link Phi0 nodes and
204                   during optimization to link to nodes that
205                   shall replace a node. */
206   /* ------- Fields for optimizations / analysis information ------- */
207   struct ir_node **out;    /**< array of out edges */
208   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
209   /* ------- For debugging ------- */
210 #ifdef DEBUG_libfirm
211   int out_valid;                /** < indicate that out edges are valid */
212   int node_nr;             /**< a unique node number for each node to make output
213                   readable. */
214 #endif
215   /* ------- For analyses -------- */
216   ir_loop *loop;           /**< the loop the node is in. Access routines in irloop.h */
217   /* ------- Opcode depending fields -------- */
218   attr attr;               /**< attribute of this node. Depends on opcode.
219                               Must be last field of struct ir_node. */
220 };
221
222 /** Copies all attributes stored in the old node  to the new node.
223     Assumes both have the same opcode and sufficient size. */
224 void
225 copy_attrs(const ir_node *old_node, ir_node *new_node);
226
227
228 /** Returns the array with the ins.  The content of the array may not be
229    changed.  */
230 ir_node     **get_irn_in            (const ir_node *node);
231
232 /** @{ */
233 /** access attributes directly */
234 INLINE const_attr    get_irn_const_attr    (ir_node *node);
235 INLINE long          get_irn_proj_attr     (ir_node *node);
236 INLINE alloc_attr    get_irn_alloc_attr    (ir_node *node);
237 INLINE type         *get_irn_free_attr     (ir_node *node);
238 INLINE symconst_attr get_irn_symconst_attr (ir_node *node);
239 type         *get_irn_call_attr     (ir_node *node);
240 type         *get_irn_funccall_attr (ir_node *node);
241 sel_attr      get_irn_sel_attr      (ir_node *node);
242 int           get_irn_phi_attr      (ir_node *node);
243 block_attr    get_irn_block_attr   (ir_node *node);
244 /** @} */
245
246 /*-------------------------------------------------------------------*/
247 /*  These function are most used in libfirm.  Give them as static    */
248 /*  functions so they can be inlined.                                */
249 /*-------------------------------------------------------------------*/
250
251 /**
252  * Checks whether a pointer points to a ir node.
253  * Intern version for libFirm.
254  */
255 static INLINE int
256 __is_ir_node (const void *thing) {
257   return (get_kind(thing) == k_ir_node);
258 }
259
260 /**
261  * Gets the op of a node.
262  * Intern version for libFirm.
263  */
264 static INLINE ir_op *
265 __get_irn_op (const ir_node *node)
266 {
267   assert (node);
268   return node->op;
269 }
270
271 /**
272  * Gets the opcode of a node.
273  * Intern version for libFirm.
274  */
275 static INLINE opcode
276 __get_irn_opcode (const ir_node *node)
277 {
278   assert (k_ir_node == get_kind(node));
279   assert (node -> op);
280   return node->op->code;
281 }
282
283 /**
284  * Returns the number of predecessors without the block predecessor.
285  * Intern version for libFirm.
286  */
287 static INLINE int
288 __get_irn_intra_arity (const ir_node *node) {
289   assert(node);
290   return ARR_LEN(node->in) - 1;
291 }
292
293 /**
294  * Returns the number of predecessors without the block predecessor.
295  * Intern version for libFirm.
296  */
297 static INLINE int
298 __get_irn_inter_arity (const ir_node *node) {
299   assert(node);
300   if (__get_irn_opcode(node) == iro_Filter) {
301     assert(node->attr.filter.in_cg);
302     return ARR_LEN(node->attr.filter.in_cg) - 1;
303   } else if (__get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
304     return ARR_LEN(node->attr.block.in_cg) - 1;
305   }
306   return __get_irn_intra_arity(node);
307 }
308
309 /**
310  * Returns the number of predecessors without the block predecessor.
311  * Intern version for libFirm.
312  */
313 static INLINE int
314 __get_irn_arity (const ir_node *node) {
315   assert(node);
316   if (interprocedural_view) return __get_irn_inter_arity(node);
317   return __get_irn_intra_arity(node);
318 }
319
320 /**
321  * Intern version for libFirm.
322  */
323 static INLINE ir_node *
324 __get_irn_intra_n (ir_node *node, int n) {
325   return (node->in[n + 1] = skip_nop(node->in[n + 1]));
326 }
327
328 /**
329  * Intern version for libFirm.
330  */
331 static INLINE ir_node*
332 __get_irn_inter_n (ir_node *node, int n) {
333   /* handle Filter and Block specially */
334   if (__get_irn_opcode(node) == iro_Filter) {
335     assert(node->attr.filter.in_cg);
336     return (node->attr.filter.in_cg[n + 1] = skip_nop(node->attr.filter.in_cg[n + 1]));
337   } else if (__get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
338     return (node->attr.block.in_cg[n + 1] = skip_nop(node->attr.block.in_cg[n + 1]));
339   }
340
341   return __get_irn_intra_n (node, n);
342 }
343
344 /**
345  * Access to the predecessors of a node.
346  * To iterate over the operands iterate from 0 to i < get_irn_arity(),
347  * to iterate including the Block predecessor iterate from i = -1 to
348  * i < get_irn_arity.
349  * If it is a block, the entry -1 is NULL.
350  * Intern version for libFirm.
351  */
352 static INLINE ir_node *
353 __get_irn_n (ir_node *node, int n) {
354   assert(node); assert(-1 <= n && n < __get_irn_arity(node));
355   if (interprocedural_view)  return __get_irn_inter_n (node, n);
356   return __get_irn_intra_n (node, n);
357 }
358
359 /**
360  * Gets the mode of a node.
361  * Intern version for libFirm.
362  */
363 static INLINE ir_mode *
364 __get_irn_mode (const ir_node *node)
365 {
366   assert (node);
367   return node->mode;
368 }
369
370 /**
371  * Sets the mode of a node.
372  * Intern version of libFirm.
373  */
374 static INLINE void
375 __set_irn_mode (ir_node *node, ir_mode *mode)
376 {
377   assert (node);
378   node->mode = mode;
379 }
380
381 /**
382  * Gets the visited counter of a node.
383  * Intern version for libFirm.
384  */
385 static INLINE unsigned long
386 __get_irn_visited (const ir_node *node)
387 {
388   assert (node);
389   return node->visited;
390 }
391
392 /**
393  * Sets the visited counter of a node.
394  * Intern version for libFirm.
395  */
396 static INLINE void
397 __set_irn_visited (ir_node *node, unsigned long visited)
398 {
399   assert (node);
400   node->visited = visited;
401 }
402
403 /**
404  * Mark a node as visited in a graph.
405  * Intern version for libFirm.
406  */
407 static INLINE void
408 __mark_irn_visited (ir_node *node) {
409   assert (node);
410   node->visited = current_ir_graph->visited;
411 }
412
413 /**
414  * Returns non-zero if a node of was visited.
415  * Intern version for libFirm.
416  */
417 static INLINE int
418 __irn_visited(const ir_node *node) {
419   assert (node);
420   return (node->visited >= current_ir_graph->visited);
421 }
422
423 /**
424  * Returns non-zero if a node of was NOT visited.
425  * Intern version for libFirm.
426  */
427 static INLINE int
428 __irn_not_visited(const ir_node *node) {
429   assert (node);
430   return (node->visited < current_ir_graph->visited);
431 }
432
433 /**
434  * Sets the link of a node.
435  * Intern version of libFirm.
436  */
437 static INLINE void
438 __set_irn_link(ir_node *node, void *link) {
439   assert (node);
440   /* Link field is used for Phi construction and various optimizations
441      in iropt. */
442   assert(get_irg_phase_state(current_ir_graph) != phase_building);
443
444   node->link = link;
445 }
446
447 /**
448  * Returns the link of a node.
449  * Intern version of libFirm.
450  */
451 static INLINE void *
452 __get_irn_link(const ir_node *node) {
453   assert (node);
454   return node->link;
455 }
456
457 /* this section MUST contain all inline functions */
458 #define is_ir_node(thing)          __is_ir_node(thing)
459 #define get_irn_intra_arity(node)  __get_irn_intra_arity(node)
460 #define get_irn_inter_arity(node)  __get_irn_inter_arity(node)
461 #define get_irn_arity(node)        __get_irn_arity(node)
462 #define get_irn_intra_n(node, n)   __get_irn_intra_n(node, n)
463 #define get_irn_inter_n(node, n)   __get_irn_inter_n(node, n)
464 #define get_irn_n(node, n)         __get_irn_n(node, n)
465 #define get_irn_mode(node)         __get_irn_mode(node)
466 #define set_irn_mode(node, mode)   __set_irn_mode(node, mode)
467 #define get_irn_op(node)           __get_irn_op(node)
468 #define get_irn_opcode(node)       __get_irn_opcode(node)
469 #define get_irn_visited(node)      __get_irn_visited(node)
470 #define set_irn_visited(node, v)   __set_irn_visited(node, v)
471 #define mark_irn_visited(node)     __mark_irn_visited(node)
472 #define irn_visited(node)          __irn_visited(node)
473 #define irn_not_visited(node)      __irn_not_visited(node)
474 #define set_irn_link(node, link)   __set_irn_link(node, link)
475 #define get_irn_link(node)         __get_irn_link(node)
476
477 # endif /* _IRNODE_T_H_ */