- dead_node_elimination: copies now node numbers, make debugging much easier
[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 #ifdef  DO_HEAPANALYSIS
218   struct abstval *av;
219   struct section *sec;
220 #endif
221   /* ------- Opcode depending fields -------- */
222   attr attr;               /**< attribute of this node. Depends on opcode.
223                               Must be last field of struct ir_node. */
224 };
225
226 /** Copies all attributes stored in the old node  to the new node.
227     Assumes both have the same opcode and sufficient size. */
228 void
229 copy_attrs(const ir_node *old_node, ir_node *new_node);
230
231
232 /** Returns the array with the ins.  The content of the array may not be
233    changed.  */
234 ir_node     **get_irn_in            (const ir_node *node);
235
236 /** @{ */
237 /** access attributes directly */
238 INLINE const_attr    get_irn_const_attr    (ir_node *node);
239 INLINE long          get_irn_proj_attr     (ir_node *node);
240 INLINE alloc_attr    get_irn_alloc_attr    (ir_node *node);
241 INLINE type         *get_irn_free_attr     (ir_node *node);
242 INLINE symconst_attr get_irn_symconst_attr (ir_node *node);
243 type         *get_irn_call_attr     (ir_node *node);
244 type         *get_irn_funccall_attr (ir_node *node);
245 sel_attr      get_irn_sel_attr      (ir_node *node);
246 int           get_irn_phi_attr      (ir_node *node);
247 block_attr    get_irn_block_attr   (ir_node *node);
248 /** @} */
249
250 /*-------------------------------------------------------------------*/
251 /*  These function are most used in libfirm.  Give them as static    */
252 /*  functions so they can be inlined.                                */
253 /*-------------------------------------------------------------------*/
254
255 /**
256  * Checks whether a pointer points to a ir node.
257  * Intern version for libFirm.
258  */
259 static INLINE int
260 __is_ir_node (const void *thing) {
261   return (get_kind(thing) == k_ir_node);
262 }
263
264 /**
265  * Gets the op of a node.
266  * Intern version for libFirm.
267  */
268 static INLINE ir_op *
269 __get_irn_op (const ir_node *node)
270 {
271   assert (node);
272   return node->op;
273 }
274
275 /**
276  * Gets the opcode of a node.
277  * Intern version for libFirm.
278  */
279 static INLINE opcode
280 __get_irn_opcode (const ir_node *node)
281 {
282   assert (k_ir_node == get_kind(node));
283   assert (node -> op);
284   return node->op->code;
285 }
286
287 /**
288  * Returns the number of predecessors without the block predecessor.
289  * Intern version for libFirm.
290  */
291 static INLINE int
292 __get_irn_intra_arity (const ir_node *node) {
293   assert(node);
294   return ARR_LEN(node->in) - 1;
295 }
296
297 /**
298  * Returns the number of predecessors without the block predecessor.
299  * Intern version for libFirm.
300  */
301 static INLINE int
302 __get_irn_inter_arity (const ir_node *node) {
303   assert(node);
304   if (__get_irn_opcode(node) == iro_Filter) {
305     assert(node->attr.filter.in_cg);
306     return ARR_LEN(node->attr.filter.in_cg) - 1;
307   } else if (__get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
308     return ARR_LEN(node->attr.block.in_cg) - 1;
309   }
310   return __get_irn_intra_arity(node);
311 }
312
313 /**
314  * Returns the number of predecessors without the block predecessor.
315  * Intern version for libFirm.
316  */
317 static INLINE int
318 __get_irn_arity (const ir_node *node) {
319   assert(node);
320   if (interprocedural_view) return __get_irn_inter_arity(node);
321   return __get_irn_intra_arity(node);
322 }
323
324 /**
325  * Intern version for libFirm.
326  */
327 static INLINE ir_node *
328 __get_irn_intra_n (ir_node *node, int n) {
329   return (node->in[n + 1] = skip_Id(node->in[n + 1]));
330 }
331
332 /**
333  * Intern version for libFirm.
334  */
335 static INLINE ir_node*
336 __get_irn_inter_n (ir_node *node, int n) {
337   /* handle Filter and Block specially */
338   if (__get_irn_opcode(node) == iro_Filter) {
339     assert(node->attr.filter.in_cg);
340     return (node->attr.filter.in_cg[n + 1] = skip_Id(node->attr.filter.in_cg[n + 1]));
341   } else if (__get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
342     return (node->attr.block.in_cg[n + 1] = skip_Id(node->attr.block.in_cg[n + 1]));
343   }
344
345   return __get_irn_intra_n (node, n);
346 }
347
348 /**
349  * Access to the predecessors of a node.
350  * To iterate over the operands iterate from 0 to i < get_irn_arity(),
351  * to iterate including the Block predecessor iterate from i = -1 to
352  * i < get_irn_arity.
353  * If it is a block, the entry -1 is NULL.
354  * Intern version for libFirm.
355  */
356 static INLINE ir_node *
357 __get_irn_n (ir_node *node, int n) {
358   assert(node); assert(-1 <= n && n < __get_irn_arity(node));
359   if (interprocedural_view)  return __get_irn_inter_n (node, n);
360   return __get_irn_intra_n (node, n);
361 }
362
363 /**
364  * Gets the mode of a node.
365  * Intern version for libFirm.
366  */
367 static INLINE ir_mode *
368 __get_irn_mode (const ir_node *node)
369 {
370   assert (node);
371   return node->mode;
372 }
373
374 /**
375  * Sets the mode of a node.
376  * Intern version of libFirm.
377  */
378 static INLINE void
379 __set_irn_mode (ir_node *node, ir_mode *mode)
380 {
381   assert (node);
382   node->mode = mode;
383 }
384
385 /**
386  * Gets the visited counter of a node.
387  * Intern version for libFirm.
388  */
389 static INLINE unsigned long
390 __get_irn_visited (const ir_node *node)
391 {
392   assert (node);
393   return node->visited;
394 }
395
396 /**
397  * Sets the visited counter of a node.
398  * Intern version for libFirm.
399  */
400 static INLINE void
401 __set_irn_visited (ir_node *node, unsigned long visited)
402 {
403   assert (node);
404   node->visited = visited;
405 }
406
407 /**
408  * Mark a node as visited in a graph.
409  * Intern version for libFirm.
410  */
411 static INLINE void
412 __mark_irn_visited (ir_node *node) {
413   assert (node);
414   node->visited = current_ir_graph->visited;
415 }
416
417 /**
418  * Returns non-zero if a node of was visited.
419  * Intern version for libFirm.
420  */
421 static INLINE int
422 __irn_visited(const ir_node *node) {
423   assert (node);
424   return (node->visited >= current_ir_graph->visited);
425 }
426
427 /**
428  * Returns non-zero if a node of was NOT visited.
429  * Intern version for libFirm.
430  */
431 static INLINE int
432 __irn_not_visited(const ir_node *node) {
433   assert (node);
434   return (node->visited < current_ir_graph->visited);
435 }
436
437 /**
438  * Sets the link of a node.
439  * Intern version of libFirm.
440  */
441 static INLINE void
442 __set_irn_link(ir_node *node, void *link) {
443   assert (node);
444   /* Link field is used for Phi construction and various optimizations
445      in iropt. */
446   assert(get_irg_phase_state(current_ir_graph) != phase_building);
447
448   node->link = link;
449 }
450
451 /**
452  * Returns the link of a node.
453  * Intern version of libFirm.
454  */
455 static INLINE void *
456 __get_irn_link(const ir_node *node) {
457   assert (node);
458   return node->link;
459 }
460
461 /* this section MUST contain all inline functions */
462 #define is_ir_node(thing)          __is_ir_node(thing)
463 #define get_irn_intra_arity(node)  __get_irn_intra_arity(node)
464 #define get_irn_inter_arity(node)  __get_irn_inter_arity(node)
465 #define get_irn_arity(node)        __get_irn_arity(node)
466 #define get_irn_intra_n(node, n)   __get_irn_intra_n(node, n)
467 #define get_irn_inter_n(node, n)   __get_irn_inter_n(node, n)
468 #define get_irn_n(node, n)         __get_irn_n(node, n)
469 #define get_irn_mode(node)         __get_irn_mode(node)
470 #define set_irn_mode(node, mode)   __set_irn_mode(node, mode)
471 #define get_irn_op(node)           __get_irn_op(node)
472 #define get_irn_opcode(node)       __get_irn_opcode(node)
473 #define get_irn_visited(node)      __get_irn_visited(node)
474 #define set_irn_visited(node, v)   __set_irn_visited(node, v)
475 #define mark_irn_visited(node)     __mark_irn_visited(node)
476 #define irn_visited(node)          __irn_visited(node)
477 #define irn_not_visited(node)      __irn_not_visited(node)
478 #define set_irn_link(node, link)   __set_irn_link(node, link)
479 #define get_irn_link(node)         __get_irn_link(node)
480
481 # endif /* _IRNODE_T_H_ */